src/hotspot/share/gc/shared/oopStorage.cpp
author kbarrett
Tue, 02 Jul 2019 18:24:47 -0400
changeset 55569 8e3a0ebf3497
parent 54663 f03d5a093093
child 55592 fd1a4c4b7616
permissions -rw-r--r--
8226366: Excessive ServiceThread wakeups for OopStorage cleanup Summary: Drive wakes via safepoint cleanups with interval minimums. Reviewed-by: coleenp, tschatzl
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
     1
/*
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 52421
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
     4
 *
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
     8
 *
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    13
 * accompanied this code).
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    14
 *
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    18
 *
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    21
 * questions.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    22
 *
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    23
 */
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    24
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    25
#include "precompiled.hpp"
48816
3495d6050efe 8194898: Move OopStorage inline definitions to an .inline.hpp
kbarrett
parents: 48808
diff changeset
    26
#include "gc/shared/oopStorage.inline.hpp"
3495d6050efe 8194898: Move OopStorage inline definitions to an .inline.hpp
kbarrett
parents: 48808
diff changeset
    27
#include "gc/shared/oopStorageParState.inline.hpp"
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    28
#include "logging/log.hpp"
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
    29
#include "logging/logStream.hpp"
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    30
#include "memory/allocation.inline.hpp"
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    31
#include "runtime/atomic.hpp"
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
    32
#include "runtime/globals.hpp"
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    33
#include "runtime/handles.inline.hpp"
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
    34
#include "runtime/interfaceSupport.inline.hpp"
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    35
#include "runtime/mutex.hpp"
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    36
#include "runtime/mutexLocker.hpp"
50429
83aec1d357d4 8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents: 50332
diff changeset
    37
#include "runtime/orderAccess.hpp"
55569
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
    38
#include "runtime/os.hpp"
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    39
#include "runtime/safepoint.hpp"
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    40
#include "runtime/stubRoutines.hpp"
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
    41
#include "runtime/thread.hpp"
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    42
#include "utilities/align.hpp"
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    43
#include "utilities/count_trailing_zeros.hpp"
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    44
#include "utilities/debug.hpp"
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    45
#include "utilities/globalDefinitions.hpp"
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    46
#include "utilities/macros.hpp"
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    47
#include "utilities/ostream.hpp"
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    48
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    49
OopStorage::AllocationListEntry::AllocationListEntry() : _prev(NULL), _next(NULL) {}
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    50
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    51
OopStorage::AllocationListEntry::~AllocationListEntry() {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    52
  assert(_prev == NULL, "deleting attached block");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    53
  assert(_next == NULL, "deleting attached block");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    54
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    55
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    56
OopStorage::AllocationList::AllocationList() : _head(NULL), _tail(NULL) {}
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    57
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    58
OopStorage::AllocationList::~AllocationList() {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    59
  // ~OopStorage() empties its lists before destroying them.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    60
  assert(_head == NULL, "deleting non-empty block list");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    61
  assert(_tail == NULL, "deleting non-empty block list");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    62
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    63
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    64
void OopStorage::AllocationList::push_front(const Block& block) {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    65
  const Block* old = _head;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    66
  if (old == NULL) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    67
    assert(_tail == NULL, "invariant");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    68
    _head = _tail = █
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    69
  } else {
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    70
    block.allocation_list_entry()._next = old;
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    71
    old->allocation_list_entry()._prev = █
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    72
    _head = █
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    73
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    74
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    75
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    76
void OopStorage::AllocationList::push_back(const Block& block) {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    77
  const Block* old = _tail;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    78
  if (old == NULL) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    79
    assert(_head == NULL, "invariant");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    80
    _head = _tail = █
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    81
  } else {
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    82
    old->allocation_list_entry()._next = █
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    83
    block.allocation_list_entry()._prev = old;
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    84
    _tail = █
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    85
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    86
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    87
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    88
void OopStorage::AllocationList::unlink(const Block& block) {
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
    89
  const AllocationListEntry& block_entry = block.allocation_list_entry();
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    90
  const Block* prev_blk = block_entry._prev;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    91
  const Block* next_blk = block_entry._next;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    92
  block_entry._prev = NULL;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    93
  block_entry._next = NULL;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    94
  if ((prev_blk == NULL) && (next_blk == NULL)) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    95
    assert(_head == &block, "invariant");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    96
    assert(_tail == &block, "invariant");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    97
    _head = _tail = NULL;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    98
  } else if (prev_blk == NULL) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
    99
    assert(_head == &block, "invariant");
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   100
    next_blk->allocation_list_entry()._prev = NULL;
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   101
    _head = next_blk;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   102
  } else if (next_blk == NULL) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   103
    assert(_tail == &block, "invariant");
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   104
    prev_blk->allocation_list_entry()._next = NULL;
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   105
    _tail = prev_blk;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   106
  } else {
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   107
    next_blk->allocation_list_entry()._prev = prev_blk;
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   108
    prev_blk->allocation_list_entry()._next = next_blk;
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   109
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   110
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   111
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   112
OopStorage::ActiveArray::ActiveArray(size_t size) :
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   113
  _size(size),
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   114
  _block_count(0),
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   115
  _refcount(0)
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   116
{}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   117
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   118
OopStorage::ActiveArray::~ActiveArray() {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   119
  assert(_refcount == 0, "precondition");
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   120
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   121
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   122
OopStorage::ActiveArray* OopStorage::ActiveArray::create(size_t size, AllocFailType alloc_fail) {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   123
  size_t size_in_bytes = blocks_offset() + sizeof(Block*) * size;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   124
  void* mem = NEW_C_HEAP_ARRAY3(char, size_in_bytes, mtGC, CURRENT_PC, alloc_fail);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   125
  if (mem == NULL) return NULL;
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   126
  return new (mem) ActiveArray(size);
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   127
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   128
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   129
void OopStorage::ActiveArray::destroy(ActiveArray* ba) {
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   130
  ba->~ActiveArray();
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   131
  FREE_C_HEAP_ARRAY(char, ba);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   132
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   133
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   134
size_t OopStorage::ActiveArray::size() const {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   135
  return _size;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   136
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   137
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   138
size_t OopStorage::ActiveArray::block_count() const {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   139
  return _block_count;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   140
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   141
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   142
size_t OopStorage::ActiveArray::block_count_acquire() const {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   143
  return OrderAccess::load_acquire(&_block_count);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   144
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   145
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   146
void OopStorage::ActiveArray::increment_refcount() const {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   147
  int new_value = Atomic::add(1, &_refcount);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   148
  assert(new_value >= 1, "negative refcount %d", new_value - 1);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   149
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   150
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   151
bool OopStorage::ActiveArray::decrement_refcount() const {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   152
  int new_value = Atomic::sub(1, &_refcount);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   153
  assert(new_value >= 0, "negative refcount %d", new_value);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   154
  return new_value == 0;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   155
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   156
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   157
bool OopStorage::ActiveArray::push(Block* block) {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   158
  size_t index = _block_count;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   159
  if (index < _size) {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   160
    block->set_active_index(index);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   161
    *block_ptr(index) = block;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   162
    // Use a release_store to ensure all the setup is complete before
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   163
    // making the block visible.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   164
    OrderAccess::release_store(&_block_count, index + 1);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   165
    return true;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   166
  } else {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   167
    return false;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   168
  }
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   169
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   170
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   171
void OopStorage::ActiveArray::remove(Block* block) {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   172
  assert(_block_count > 0, "array is empty");
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   173
  size_t index = block->active_index();
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   174
  assert(*block_ptr(index) == block, "block not present");
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   175
  size_t last_index = _block_count - 1;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   176
  Block* last_block = *block_ptr(last_index);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   177
  last_block->set_active_index(index);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   178
  *block_ptr(index) = last_block;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   179
  _block_count = last_index;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   180
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   181
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   182
void OopStorage::ActiveArray::copy_from(const ActiveArray* from) {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   183
  assert(_block_count == 0, "array must be empty");
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   184
  size_t count = from->_block_count;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   185
  assert(count <= _size, "precondition");
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   186
  Block* const* from_ptr = from->block_ptr(0);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   187
  Block** to_ptr = block_ptr(0);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   188
  for (size_t i = 0; i < count; ++i) {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   189
    Block* block = *from_ptr++;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   190
    assert(block->active_index() == i, "invariant");
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   191
    *to_ptr++ = block;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   192
  }
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   193
  _block_count = count;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   194
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   195
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   196
// Blocks start with an array of BitsPerWord oop entries.  That array
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   197
// is divided into conceptual BytesPerWord sections of BitsPerByte
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   198
// entries.  Blocks are allocated aligned on section boundaries, for
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   199
// the convenience of mapping from an entry to the containing block;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   200
// see block_for_ptr().  Aligning on section boundary rather than on
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   201
// the full _data wastes a lot less space, but makes for a bit more
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   202
// work in block_for_ptr().
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   203
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   204
const unsigned section_size = BitsPerByte;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   205
const unsigned section_count = BytesPerWord;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   206
const unsigned block_alignment = sizeof(oop) * section_size;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   207
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   208
OopStorage::Block::Block(const OopStorage* owner, void* memory) :
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   209
  _data(),
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   210
  _allocated_bitmask(0),
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   211
  _owner(owner),
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   212
  _memory(memory),
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   213
  _active_index(0),
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   214
  _allocation_list_entry(),
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   215
  _deferred_updates_next(NULL),
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   216
  _release_refcount(0)
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   217
{
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   218
  STATIC_ASSERT(_data_pos == 0);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   219
  STATIC_ASSERT(section_size * section_count == ARRAY_SIZE(_data));
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   220
  assert(offset_of(Block, _data) == _data_pos, "invariant");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   221
  assert(owner != NULL, "NULL owner");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   222
  assert(is_aligned(this, block_alignment), "misaligned block");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   223
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   224
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   225
OopStorage::Block::~Block() {
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   226
  assert(_release_refcount == 0, "deleting block while releasing");
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   227
  assert(_deferred_updates_next == NULL, "deleting block with deferred update");
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   228
  // Clear fields used by block_for_ptr and entry validation, which
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   229
  // might help catch bugs.  Volatile to prevent dead-store elimination.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   230
  const_cast<uintx volatile&>(_allocated_bitmask) = 0;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   231
  const_cast<OopStorage* volatile&>(_owner) = NULL;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   232
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   233
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   234
size_t OopStorage::Block::allocation_size() {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   235
  // _data must be first member, so aligning Block aligns _data.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   236
  STATIC_ASSERT(_data_pos == 0);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   237
  return sizeof(Block) + block_alignment - sizeof(void*);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   238
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   239
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   240
size_t OopStorage::Block::allocation_alignment_shift() {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   241
  return exact_log2(block_alignment);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   242
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   243
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   244
inline bool is_full_bitmask(uintx bitmask) { return ~bitmask == 0; }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   245
inline bool is_empty_bitmask(uintx bitmask) { return bitmask == 0; }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   246
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   247
bool OopStorage::Block::is_full() const {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   248
  return is_full_bitmask(allocated_bitmask());
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   249
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   250
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   251
bool OopStorage::Block::is_empty() const {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   252
  return is_empty_bitmask(allocated_bitmask());
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   253
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   254
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   255
uintx OopStorage::Block::bitmask_for_entry(const oop* ptr) const {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   256
  return bitmask_for_index(get_index(ptr));
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   257
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   258
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   259
// An empty block is not yet deletable if either:
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   260
// (1) There is a release() operation currently operating on it.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   261
// (2) It is in the deferred updates list.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   262
// For interaction with release(), these must follow the empty check,
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   263
// and the order of these checks is important.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   264
bool OopStorage::Block::is_safe_to_delete() const {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   265
  assert(is_empty(), "precondition");
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   266
  OrderAccess::loadload();
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   267
  return (OrderAccess::load_acquire(&_release_refcount) == 0) &&
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   268
         (OrderAccess::load_acquire(&_deferred_updates_next) == NULL);
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   269
}
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   270
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   271
OopStorage::Block* OopStorage::Block::deferred_updates_next() const {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   272
  return _deferred_updates_next;
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   273
}
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   274
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   275
void OopStorage::Block::set_deferred_updates_next(Block* block) {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   276
  _deferred_updates_next = block;
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   277
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   278
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   279
bool OopStorage::Block::contains(const oop* ptr) const {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   280
  const oop* base = get_pointer(0);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   281
  return (base <= ptr) && (ptr < (base + ARRAY_SIZE(_data)));
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   282
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   283
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   284
size_t OopStorage::Block::active_index() const {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   285
  return _active_index;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   286
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   287
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   288
void OopStorage::Block::set_active_index(size_t index) {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   289
  _active_index = index;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   290
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   291
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   292
size_t OopStorage::Block::active_index_safe(const Block* block) {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   293
  STATIC_ASSERT(sizeof(intptr_t) == sizeof(block->_active_index));
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   294
  assert(CanUseSafeFetchN(), "precondition");
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   295
  return SafeFetchN((intptr_t*)&block->_active_index, 0);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   296
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   297
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   298
unsigned OopStorage::Block::get_index(const oop* ptr) const {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   299
  assert(contains(ptr), PTR_FORMAT " not in block " PTR_FORMAT, p2i(ptr), p2i(this));
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   300
  return static_cast<unsigned>(ptr - get_pointer(0));
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   301
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   302
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   303
oop* OopStorage::Block::allocate() {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   304
  // Use CAS loop because release may change bitmask outside of lock.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   305
  uintx allocated = allocated_bitmask();
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   306
  while (true) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   307
    assert(!is_full_bitmask(allocated), "attempt to allocate from full block");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   308
    unsigned index = count_trailing_zeros(~allocated);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   309
    uintx new_value = allocated | bitmask_for_index(index);
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   310
    uintx fetched = Atomic::cmpxchg(new_value, &_allocated_bitmask, allocated);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   311
    if (fetched == allocated) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   312
      return get_pointer(index); // CAS succeeded; return entry for index.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   313
    }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   314
    allocated = fetched;       // CAS failed; retry with latest value.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   315
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   316
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   317
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   318
OopStorage::Block* OopStorage::Block::new_block(const OopStorage* owner) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   319
  // _data must be first member: aligning block => aligning _data.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   320
  STATIC_ASSERT(_data_pos == 0);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   321
  size_t size_needed = allocation_size();
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   322
  void* memory = NEW_C_HEAP_ARRAY_RETURN_NULL(char, size_needed, mtGC);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   323
  if (memory == NULL) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   324
    return NULL;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   325
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   326
  void* block_mem = align_up(memory, block_alignment);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   327
  assert(sizeof(Block) + pointer_delta(block_mem, memory, 1) <= size_needed,
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   328
         "allocated insufficient space for aligned block");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   329
  return ::new (block_mem) Block(owner, memory);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   330
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   331
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   332
void OopStorage::Block::delete_block(const Block& block) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   333
  void* memory = block._memory;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   334
  block.Block::~Block();
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   335
  FREE_C_HEAP_ARRAY(char, memory);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   336
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   337
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   338
// This can return a false positive if ptr is not contained by some
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   339
// block.  For some uses, it is a precondition that ptr is valid,
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   340
// e.g. contained in some block in owner's _active_array.  Other uses
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   341
// require additional validation of the result.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   342
OopStorage::Block*
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   343
OopStorage::Block::block_for_ptr(const OopStorage* owner, const oop* ptr) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   344
  assert(CanUseSafeFetchN(), "precondition");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   345
  STATIC_ASSERT(_data_pos == 0);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   346
  // Const-ness of ptr is not related to const-ness of containing block.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   347
  // Blocks are allocated section-aligned, so get the containing section.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   348
  oop* section_start = align_down(const_cast<oop*>(ptr), block_alignment);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   349
  // Start with a guess that the containing section is the last section,
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   350
  // so the block starts section_count-1 sections earlier.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   351
  oop* section = section_start - (section_size * (section_count - 1));
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   352
  // Walk up through the potential block start positions, looking for
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   353
  // the owner in the expected location.  If we're below the actual block
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   354
  // start position, the value at the owner position will be some oop
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   355
  // (possibly NULL), which can never match the owner.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   356
  intptr_t owner_addr = reinterpret_cast<intptr_t>(owner);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   357
  for (unsigned i = 0; i < section_count; ++i, section += section_size) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   358
    Block* candidate = reinterpret_cast<Block*>(section);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   359
    intptr_t* candidate_owner_addr
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   360
      = reinterpret_cast<intptr_t*>(&candidate->_owner);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   361
    if (SafeFetchN(candidate_owner_addr, 0) == owner_addr) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   362
      return candidate;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   363
    }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   364
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   365
  return NULL;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   366
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   367
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   368
//////////////////////////////////////////////////////////////////////////////
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   369
// Allocation
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   370
//
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   371
// Allocation involves the _allocation_list, which contains a subset of the
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   372
// blocks owned by a storage object.  This is a doubly-linked list, linked
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   373
// through dedicated fields in the blocks.  Full blocks are removed from this
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   374
// list, though they are still present in the _active_array.  Empty blocks are
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   375
// kept at the end of the _allocation_list, to make it easy for empty block
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   376
// deletion to find them.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   377
//
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   378
// allocate(), and delete_empty_blocks() lock the
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   379
// _allocation_mutex while performing any list and array modifications.
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   380
//
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   381
// allocate() and release() update a block's _allocated_bitmask using CAS
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   382
// loops.  This prevents loss of updates even though release() performs
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   383
// its updates without any locking.
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   384
//
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   385
// allocate() obtains the entry from the first block in the _allocation_list,
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   386
// and updates that block's _allocated_bitmask to indicate the entry is in
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   387
// use.  If this makes the block full (all entries in use), the block is
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   388
// removed from the _allocation_list so it won't be considered by future
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   389
// allocations until some entries in it are released.
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   390
//
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   391
// release() is performed lock-free. (Note: This means it can't notify the
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   392
// service thread of pending cleanup work.  It must be lock-free because
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   393
// it is called in all kinds of contexts where even quite low ranked locks
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   394
// may be held.)  release() first looks up the block for
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   395
// the entry, using address alignment to find the enclosing block (thereby
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   396
// avoiding iteration over the _active_array).  Once the block has been
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   397
// determined, its _allocated_bitmask needs to be updated, and its position in
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   398
// the _allocation_list may need to be updated.  There are two cases:
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   399
//
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   400
// (a) If the block is neither full nor would become empty with the release of
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   401
// the entry, only its _allocated_bitmask needs to be updated.  But if the CAS
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   402
// update fails, the applicable case may change for the retry.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   403
//
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   404
// (b) Otherwise, the _allocation_list also needs to be modified.  This requires
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   405
// locking the _allocation_mutex.  To keep the release() operation lock-free,
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   406
// rather than updating the _allocation_list itself, it instead performs a
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   407
// lock-free push of the block onto the _deferred_updates list.  Entries on
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   408
// that list are processed by allocate() and delete_empty_blocks(), while
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   409
// they already hold the necessary lock.  That processing makes the block's
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   410
// list state consistent with its current _allocated_bitmask.  The block is
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   411
// added to the _allocation_list if not already present and the bitmask is not
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   412
// full.  The block is moved to the end of the _allocation_list if the bitmask
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   413
// is empty, for ease of empty block deletion processing.
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   414
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   415
oop* OopStorage::allocate() {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 52421
diff changeset
   416
  MutexLocker ml(_allocation_mutex, Mutex::_no_safepoint_check_flag);
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   417
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   418
  Block* block = block_for_allocation();
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   419
  if (block == NULL) return NULL; // Block allocation failed.
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   420
  assert(!block->is_full(), "invariant");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   421
  if (block->is_empty()) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   422
    // Transitioning from empty to not empty.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   423
    log_debug(oopstorage, blocks)("%s: block not empty " PTR_FORMAT, name(), p2i(block));
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   424
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   425
  oop* result = block->allocate();
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   426
  assert(result != NULL, "allocation failed");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   427
  assert(!block->is_empty(), "postcondition");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   428
  Atomic::inc(&_allocation_count); // release updates outside lock.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   429
  if (block->is_full()) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   430
    // Transitioning from not full to full.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   431
    // Remove full blocks from consideration by future allocates.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   432
    log_debug(oopstorage, blocks)("%s: block full " PTR_FORMAT, name(), p2i(block));
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   433
    _allocation_list.unlink(*block);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   434
  }
51752
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
   435
  log_trace(oopstorage, ref)("%s: allocated " PTR_FORMAT, name(), p2i(result));
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   436
  return result;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   437
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   438
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   439
bool OopStorage::try_add_block() {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   440
  assert_lock_strong(_allocation_mutex);
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   441
  Block* block;
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   442
  {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 52421
diff changeset
   443
    MutexUnlocker ul(_allocation_mutex, Mutex::_no_safepoint_check_flag);
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   444
    block = Block::new_block(this);
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   445
  }
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   446
  if (block == NULL) return false;
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   447
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   448
  // Add new block to the _active_array, growing if needed.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   449
  if (!_active_array->push(block)) {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   450
    if (expand_active_array()) {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   451
      guarantee(_active_array->push(block), "push failed after expansion");
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   452
    } else {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   453
      log_debug(oopstorage, blocks)("%s: failed active array expand", name());
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   454
      Block::delete_block(*block);
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   455
      return false;
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   456
    }
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   457
  }
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   458
  // Add to end of _allocation_list.  The mutex release allowed other
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   459
  // threads to add blocks to the _allocation_list.  We prefer to
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   460
  // allocate from non-empty blocks, to allow empty blocks to be
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   461
  // deleted.  But we don't bother notifying about the empty block
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   462
  // because we're (probably) about to allocate an entry from it.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   463
  _allocation_list.push_back(*block);
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   464
  log_debug(oopstorage, blocks)("%s: new block " PTR_FORMAT, name(), p2i(block));
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   465
  return true;
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   466
}
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   467
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   468
OopStorage::Block* OopStorage::block_for_allocation() {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   469
  assert_lock_strong(_allocation_mutex);
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   470
  while (true) {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   471
    // Use the first block in _allocation_list for the allocation.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   472
    Block* block = _allocation_list.head();
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   473
    if (block != NULL) {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   474
      return block;
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   475
    } else if (reduce_deferred_updates()) {
55569
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   476
      // Might have added a block to the _allocation_list, so retry.
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   477
    } else if (try_add_block()) {
55569
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   478
      // Successfully added a new block to the list, so retry.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   479
      assert(_allocation_list.chead() != NULL, "invariant");
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   480
    } else if (_allocation_list.chead() != NULL) {
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   481
      // Trying to add a block failed, but some other thread added to the
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   482
      // list while we'd dropped the lock over the new block allocation.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   483
    } else if (!reduce_deferred_updates()) { // Once more before failure.
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   484
      // Attempt to add a block failed, no other thread added a block,
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   485
      // and no deferred updated added a block, then allocation failed.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   486
      log_debug(oopstorage, blocks)("%s: failed block allocation", name());
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   487
      return NULL;
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   488
    }
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   489
  }
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   490
}
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   491
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   492
// Create a new, larger, active array with the same content as the
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   493
// current array, and then replace, relinquishing the old array.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   494
// Return true if the array was successfully expanded, false to
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   495
// indicate allocation failure.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   496
bool OopStorage::expand_active_array() {
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   497
  assert_lock_strong(_allocation_mutex);
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   498
  ActiveArray* old_array = _active_array;
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   499
  size_t new_size = 2 * old_array->size();
51752
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
   500
  log_debug(oopstorage, blocks)("%s: expand active array " SIZE_FORMAT,
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
   501
                                name(), new_size);
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   502
  ActiveArray* new_array = ActiveArray::create(new_size, AllocFailStrategy::RETURN_NULL);
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   503
  if (new_array == NULL) return false;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   504
  new_array->copy_from(old_array);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   505
  replace_active_array(new_array);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   506
  relinquish_block_array(old_array);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   507
  return true;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   508
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   509
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   510
// Make new_array the _active_array.  Increments new_array's refcount
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   511
// to account for the new reference.  The assignment is atomic wrto
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   512
// obtain_active_array; once this function returns, it is safe for the
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   513
// caller to relinquish the old array.
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   514
void OopStorage::replace_active_array(ActiveArray* new_array) {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   515
  // Caller has the old array that is the current value of _active_array.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   516
  // Update new_array refcount to account for the new reference.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   517
  new_array->increment_refcount();
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   518
  // Install new_array, ensuring its initialization is complete first.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   519
  OrderAccess::release_store(&_active_array, new_array);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   520
  // Wait for any readers that could read the old array from _active_array.
51511
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents: 50954
diff changeset
   521
  // Can't use GlobalCounter here, because this is called from allocate(),
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents: 50954
diff changeset
   522
  // which may be called in the scope of a GlobalCounter critical section
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents: 50954
diff changeset
   523
  // when inserting a StringTable entry.
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents: 50954
diff changeset
   524
  _protect_active.synchronize();
50332
d0d933d61610 8204179: [BACKOUT] OopStorage should use GlobalCounter
kbarrett
parents: 50300
diff changeset
   525
  // All obtain critical sections that could see the old array have
d0d933d61610 8204179: [BACKOUT] OopStorage should use GlobalCounter
kbarrett
parents: 50300
diff changeset
   526
  // completed, having incremented the refcount of the old array.  The
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   527
  // caller can now safely relinquish the old array.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   528
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   529
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   530
// Atomically (wrto replace_active_array) get the active array and
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   531
// increment its refcount.  This provides safe access to the array,
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   532
// even if an allocate operation expands and replaces the value of
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   533
// _active_array.  The caller must relinquish the array when done
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   534
// using it.
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   535
OopStorage::ActiveArray* OopStorage::obtain_active_array() const {
51511
eb8d5aeabab3 8209850: Allow NamedThreads to use GlobalCounter critical sections
kbarrett
parents: 50954
diff changeset
   536
  SingleWriterSynchronizer::CriticalSection cs(&_protect_active);
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   537
  ActiveArray* result = OrderAccess::load_acquire(&_active_array);
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   538
  result->increment_refcount();
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   539
  return result;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   540
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   541
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   542
// Decrement refcount of array and destroy if refcount is zero.
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   543
void OopStorage::relinquish_block_array(ActiveArray* array) const {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   544
  if (array->decrement_refcount()) {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   545
    assert(array != _active_array, "invariant");
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   546
    ActiveArray::destroy(array);
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   547
  }
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   548
}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   549
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   550
class OopStorage::WithActiveArray : public StackObj {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   551
  const OopStorage* _storage;
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   552
  ActiveArray* _active_array;
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   553
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   554
public:
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   555
  WithActiveArray(const OopStorage* storage) :
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   556
    _storage(storage),
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   557
    _active_array(storage->obtain_active_array())
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   558
  {}
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   559
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   560
  ~WithActiveArray() {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   561
    _storage->relinquish_block_array(_active_array);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   562
  }
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   563
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   564
  ActiveArray& active_array() const {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   565
    return *_active_array;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   566
  }
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   567
};
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   568
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   569
OopStorage::Block* OopStorage::find_block_or_null(const oop* ptr) const {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   570
  assert(ptr != NULL, "precondition");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   571
  return Block::block_for_ptr(this, ptr);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   572
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   573
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   574
static void log_release_transitions(uintx releasing,
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   575
                                    uintx old_allocated,
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   576
                                    const OopStorage* owner,
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   577
                                    const void* block) {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   578
  Log(oopstorage, blocks) log;
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   579
  LogStream ls(log.debug());
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   580
  if (is_full_bitmask(old_allocated)) {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   581
    ls.print_cr("%s: block not full " PTR_FORMAT, owner->name(), p2i(block));
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   582
  }
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   583
  if (releasing == old_allocated) {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   584
    ls.print_cr("%s: block empty " PTR_FORMAT, owner->name(), p2i(block));
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   585
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   586
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   587
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   588
void OopStorage::Block::release_entries(uintx releasing, OopStorage* owner) {
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   589
  assert(releasing != 0, "preconditon");
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   590
  // Prevent empty block deletion when transitioning to empty.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   591
  Atomic::inc(&_release_refcount);
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   592
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   593
  // Atomically update allocated bitmask.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   594
  uintx old_allocated = _allocated_bitmask;
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   595
  while (true) {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   596
    assert((releasing & ~old_allocated) == 0, "releasing unallocated entries");
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   597
    uintx new_value = old_allocated ^ releasing;
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   598
    uintx fetched = Atomic::cmpxchg(new_value, &_allocated_bitmask, old_allocated);
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   599
    if (fetched == old_allocated) break; // Successful update.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   600
    old_allocated = fetched;             // Retry with updated bitmask.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   601
  }
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   602
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   603
  // Now that the bitmask has been updated, if we have a state transition
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   604
  // (updated bitmask is empty or old bitmask was full), atomically push
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   605
  // this block onto the deferred updates list.  Some future call to
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   606
  // reduce_deferred_updates will make any needed changes related to this
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   607
  // block and _allocation_list.  This deferral avoids _allocation_list
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   608
  // updates and the associated locking here.
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   609
  if ((releasing == old_allocated) || is_full_bitmask(old_allocated)) {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   610
    // Log transitions.  Both transitions are possible in a single update.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   611
    if (log_is_enabled(Debug, oopstorage, blocks)) {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   612
      log_release_transitions(releasing, old_allocated, _owner, this);
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   613
    }
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   614
    // Attempt to claim responsibility for adding this block to the deferred
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   615
    // list, by setting the link to non-NULL by self-looping.  If this fails,
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   616
    // then someone else has made such a claim and the deferred update has not
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   617
    // yet been processed and will include our change, so we don't need to do
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   618
    // anything further.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   619
    if (Atomic::replace_if_null(this, &_deferred_updates_next)) {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   620
      // Successfully claimed.  Push, with self-loop for end-of-list.
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   621
      Block* head = owner->_deferred_updates;
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   622
      while (true) {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   623
        _deferred_updates_next = (head == NULL) ? this : head;
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   624
        Block* fetched = Atomic::cmpxchg(this, &owner->_deferred_updates, head);
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   625
        if (fetched == head) break; // Successful update.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   626
        head = fetched;             // Retry with updated head.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   627
      }
55569
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   628
      // Only request cleanup for to-empty transitions, not for from-full.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   629
      // There isn't any rush to process from-full transitions.  Allocation
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   630
      // will reduce deferrals before allocating new blocks, so may process
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   631
      // some.  And the service thread will drain the entire deferred list
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   632
      // if there are any pending to-empty transitions.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   633
      if (releasing == old_allocated) {
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   634
        owner->record_needs_cleanup();
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   635
      }
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   636
      log_debug(oopstorage, blocks)("%s: deferred update " PTR_FORMAT,
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   637
                                    _owner->name(), p2i(this));
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   638
    }
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   639
  }
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   640
  // Release hold on empty block deletion.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   641
  Atomic::dec(&_release_refcount);
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   642
}
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   643
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   644
// Process one available deferred update.  Returns true if one was processed.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   645
bool OopStorage::reduce_deferred_updates() {
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   646
  assert_lock_strong(_allocation_mutex);
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   647
  // Atomically pop a block off the list, if any available.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   648
  // No ABA issue because this is only called by one thread at a time.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   649
  // The atomicity is wrto pushes by release().
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   650
  Block* block = OrderAccess::load_acquire(&_deferred_updates);
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   651
  while (true) {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   652
    if (block == NULL) return false;
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   653
    // Try atomic pop of block from list.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   654
    Block* tail = block->deferred_updates_next();
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   655
    if (block == tail) tail = NULL; // Handle self-loop end marker.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   656
    Block* fetched = Atomic::cmpxchg(tail, &_deferred_updates, block);
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   657
    if (fetched == block) break; // Update successful.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   658
    block = fetched;             // Retry with updated block.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   659
  }
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   660
  block->set_deferred_updates_next(NULL); // Clear tail after updating head.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   661
  // Ensure bitmask read after pop is complete, including clearing tail, for
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   662
  // ordering with release().  Without this, we may be processing a stale
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   663
  // bitmask state here while blocking a release() operation from recording
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   664
  // the deferred update needed for its bitmask change.
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   665
  OrderAccess::fence();
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   666
  // Process popped block.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   667
  uintx allocated = block->allocated_bitmask();
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   668
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   669
  // Make membership in list consistent with bitmask state.
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   670
  if ((_allocation_list.ctail() != NULL) &&
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   671
      ((_allocation_list.ctail() == block) ||
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   672
       (_allocation_list.next(*block) != NULL))) {
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   673
    // Block is in the _allocation_list.
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   674
    assert(!is_full_bitmask(allocated), "invariant");
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   675
  } else if (!is_full_bitmask(allocated)) {
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   676
    // Block is not in the _allocation_list, but now should be.
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   677
    _allocation_list.push_front(*block);
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   678
  } // Else block is full and not in list, which is correct.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   679
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   680
  // Move empty block to end of list, for possible deletion.
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   681
  if (is_empty_bitmask(allocated)) {
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   682
    _allocation_list.unlink(*block);
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   683
    _allocation_list.push_back(*block);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   684
  }
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   685
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   686
  log_debug(oopstorage, blocks)("%s: processed deferred update " PTR_FORMAT,
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   687
                                name(), p2i(block));
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   688
  return true;              // Processed one pending update.
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   689
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   690
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   691
inline void check_release_entry(const oop* entry) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   692
  assert(entry != NULL, "Releasing NULL");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   693
  assert(*entry == NULL, "Releasing uncleared entry: " PTR_FORMAT, p2i(entry));
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   694
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   695
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   696
void OopStorage::release(const oop* ptr) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   697
  check_release_entry(ptr);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   698
  Block* block = find_block_or_null(ptr);
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   699
  assert(block != NULL, "%s: invalid release " PTR_FORMAT, name(), p2i(ptr));
51752
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
   700
  log_trace(oopstorage, ref)("%s: released " PTR_FORMAT, name(), p2i(ptr));
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   701
  block->release_entries(block->bitmask_for_entry(ptr), this);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   702
  Atomic::dec(&_allocation_count);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   703
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   704
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   705
void OopStorage::release(const oop* const* ptrs, size_t size) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   706
  size_t i = 0;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   707
  while (i < size) {
48808
2b0b7f222800 8195690: JNI GetObjectRefType doesn't handle NULL
kbarrett
parents: 48787
diff changeset
   708
    check_release_entry(ptrs[i]);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   709
    Block* block = find_block_or_null(ptrs[i]);
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   710
    assert(block != NULL, "%s: invalid release " PTR_FORMAT, name(), p2i(ptrs[i]));
51752
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
   711
    log_trace(oopstorage, ref)("%s: released " PTR_FORMAT, name(), p2i(ptrs[i]));
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   712
    size_t count = 0;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   713
    uintx releasing = 0;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   714
    for ( ; i < size; ++i) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   715
      const oop* entry = ptrs[i];
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   716
      check_release_entry(entry);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   717
      // If entry not in block, finish block and resume outer loop with entry.
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   718
      if (!block->contains(entry)) break;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   719
      // Add entry to releasing bitmap.
51752
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
   720
      log_trace(oopstorage, ref)("%s: released " PTR_FORMAT, name(), p2i(entry));
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   721
      uintx entry_bitmask = block->bitmask_for_entry(entry);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   722
      assert((releasing & entry_bitmask) == 0,
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   723
             "Duplicate entry: " PTR_FORMAT, p2i(entry));
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   724
      releasing |= entry_bitmask;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   725
      ++count;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   726
    }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   727
    // Release the contiguous entries that are in block.
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   728
    block->release_entries(releasing, this);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   729
    Atomic::sub(count, &_allocation_count);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   730
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   731
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   732
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   733
const char* dup_name(const char* name) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   734
  char* dup = NEW_C_HEAP_ARRAY(char, strlen(name) + 1, mtGC);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   735
  strcpy(dup, name);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   736
  return dup;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   737
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   738
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   739
const size_t initial_active_array_size = 8;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   740
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   741
OopStorage::OopStorage(const char* name,
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   742
                       Mutex* allocation_mutex,
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   743
                       Mutex* active_mutex) :
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   744
  _name(dup_name(name)),
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   745
  _active_array(ActiveArray::create(initial_active_array_size)),
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   746
  _allocation_list(),
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   747
  _deferred_updates(NULL),
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   748
  _allocation_mutex(allocation_mutex),
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   749
  _active_mutex(active_mutex),
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   750
  _allocation_count(0),
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   751
  _concurrent_iteration_count(0),
55569
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   752
  _needs_cleanup(false)
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   753
{
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   754
  _active_array->increment_refcount();
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   755
  assert(_active_mutex->rank() < _allocation_mutex->rank(),
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   756
         "%s: active_mutex must have lower rank than allocation_mutex", _name);
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   757
  assert(Service_lock->rank() < _active_mutex->rank(),
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   758
         "%s: active_mutex must have higher rank than Service_lock", _name);
54663
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   759
  assert(_active_mutex->_safepoint_check_required == Mutex::_safepoint_check_never,
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   760
         "%s: active mutex requires never safepoint check", _name);
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   761
  assert(_allocation_mutex->_safepoint_check_required == Mutex::_safepoint_check_never,
f03d5a093093 8074355: make MutexLocker smarter about non-JavaThreads
coleenp
parents: 54623
diff changeset
   762
         "%s: allocation mutex requires never safepoint check", _name);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   763
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   764
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   765
void OopStorage::delete_empty_block(const Block& block) {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   766
  assert(block.is_empty(), "discarding non-empty block");
51752
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
   767
  log_debug(oopstorage, blocks)("%s: delete empty block " PTR_FORMAT, name(), p2i(&block));
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   768
  Block::delete_block(block);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   769
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   770
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   771
OopStorage::~OopStorage() {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   772
  Block* block;
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   773
  while ((block = _deferred_updates) != NULL) {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   774
    _deferred_updates = block->deferred_updates_next();
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   775
    block->set_deferred_updates_next(NULL);
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   776
  }
50954
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   777
  while ((block = _allocation_list.head()) != NULL) {
f85092465b0c 8204834: Fix confusing "allocate" naming in OopStorage
kbarrett
parents: 50513
diff changeset
   778
    _allocation_list.unlink(*block);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   779
  }
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   780
  bool unreferenced = _active_array->decrement_refcount();
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   781
  assert(unreferenced, "deleting storage while _active_array is referenced");
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   782
  for (size_t i = _active_array->block_count(); 0 < i; ) {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   783
    block = _active_array->at(--i);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   784
    Block::delete_block(*block);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   785
  }
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   786
  ActiveArray::destroy(_active_array);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   787
  FREE_C_HEAP_ARRAY(char, _name);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   788
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   789
55569
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   790
// Managing service thread notifications.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   791
//
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   792
// We don't want cleanup work to linger indefinitely, but we also don't want
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   793
// to run the service thread too often.  We're also very limited in what we
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   794
// can do in a release operation, where cleanup work is created.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   795
//
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   796
// When a release operation changes a block's state to empty, it records the
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   797
// need for cleanup in both the associated storage object and in the global
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   798
// request state.  A safepoint cleanup task notifies the service thread when
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   799
// there may be cleanup work for any storage object, based on the global
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   800
// request state.  But that notification is deferred if the service thread
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   801
// has run recently, and we also avoid duplicate notifications.  The service
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   802
// thread updates the timestamp and resets the state flags on every iteration.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   803
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   804
// Global cleanup request state.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   805
static volatile bool needs_cleanup_requested = false;
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   806
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   807
// Flag for avoiding duplicate notifications.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   808
static bool needs_cleanup_triggered = false;
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   809
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   810
// Time after which a notification can be made.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   811
static jlong cleanup_trigger_permit_time = 0;
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   812
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   813
// Minimum time since last service thread check before notification is
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   814
// permitted.  The value of 500ms was an arbitrary choice; frequent, but not
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   815
// too frequent.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   816
const jlong cleanup_trigger_defer_period = 500 * NANOSECS_PER_MILLISEC;
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   817
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   818
void OopStorage::trigger_cleanup_if_needed() {
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   819
  MonitorLocker ml(Service_lock, Monitor::_no_safepoint_check_flag);
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   820
  if (Atomic::load(&needs_cleanup_requested) &&
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   821
      !needs_cleanup_triggered &&
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   822
      (os::javaTimeNanos() > cleanup_trigger_permit_time)) {
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   823
    needs_cleanup_triggered = true;
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   824
    ml.notify_all();
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   825
  }
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   826
}
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   827
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   828
bool OopStorage::has_cleanup_work_and_reset() {
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   829
  assert_lock_strong(Service_lock);
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   830
  cleanup_trigger_permit_time =
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   831
    os::javaTimeNanos() + cleanup_trigger_defer_period;
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   832
  needs_cleanup_triggered = false;
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   833
  // Set the request flag false and return its old value.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   834
  // Needs to be atomic to avoid dropping a concurrent request.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   835
  // Can't use Atomic::xchg, which may not support bool.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   836
  return Atomic::cmpxchg(false, &needs_cleanup_requested, true);
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   837
}
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   838
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   839
// Record that cleanup is needed, without notifying the Service thread.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   840
// Used by release(), where we can't lock even Service_lock.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   841
void OopStorage::record_needs_cleanup() {
55569
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   842
  // Set local flag first, else service thread could wake up and miss
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   843
  // the request.  This order may instead (rarely) unnecessarily notify.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   844
  OrderAccess::release_store(&_needs_cleanup, true);
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   845
  OrderAccess::release_store_fence(&needs_cleanup_requested, true);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   846
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   847
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   848
bool OopStorage::delete_empty_blocks() {
55569
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   849
  // Service thread might have oopstorage work, but not for this object.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   850
  // Check for deferred updates even though that's not a service thread
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   851
  // trigger; since we're here, we might as well process them.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   852
  if (!OrderAccess::load_acquire(&_needs_cleanup) &&
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   853
      (OrderAccess::load_acquire(&_deferred_updates) == NULL)) {
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   854
    return false;
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   855
  }
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   856
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 52421
diff changeset
   857
  MutexLocker ml(_allocation_mutex, Mutex::_no_safepoint_check_flag);
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   858
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   859
  // Clear the request before processing.
55569
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   860
  OrderAccess::release_store_fence(&_needs_cleanup, false);
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   861
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   862
  // Other threads could be adding to the empty block count or the
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   863
  // deferred update list while we're working.  Set an upper bound on
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   864
  // how many updates we'll process and blocks we'll try to release,
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   865
  // so other threads can't cause an unbounded stay in this function.
55569
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   866
  // We add a bit of slop because the reduce_deferred_updates clause
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   867
  // can cause blocks to be double counted.  If there are few blocks
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   868
  // and many of them are deferred and empty, we might hit the limit
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   869
  // and spin the caller without doing very much work.  Otherwise,
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   870
  // we don't normally hit the limit anyway, instead running out of
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   871
  // work to do.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   872
  size_t limit = block_count() + 10;
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   873
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   874
  for (size_t i = 0; i < limit; ++i) {
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   875
    // Process deferred updates, which might make empty blocks available.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   876
    // Continue checking once deletion starts, since additional updates
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   877
    // might become available while we're working.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   878
    if (reduce_deferred_updates()) {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   879
      // Be safepoint-polite while looping.
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 52421
diff changeset
   880
      MutexUnlocker ul(_allocation_mutex, Mutex::_no_safepoint_check_flag);
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   881
      ThreadBlockInVM tbiv(JavaThread::current());
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   882
    } else {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   883
      Block* block = _allocation_list.tail();
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   884
      if ((block == NULL) || !block->is_empty()) {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   885
        return false;
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   886
      } else if (!block->is_safe_to_delete()) {
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   887
        // Look for other work while waiting for block to be deletable.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   888
        break;
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   889
      }
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   890
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   891
      // Try to delete the block.  First, try to remove from _active_array.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   892
      {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 52421
diff changeset
   893
        MutexLocker aml(_active_mutex, Mutex::_no_safepoint_check_flag);
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   894
        // Don't interfere with an active concurrent iteration.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   895
        // Instead, give up immediately.  There is more work to do,
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   896
        // but don't re-notify, to avoid useless spinning of the
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   897
        // service thread.  Instead, iteration completion notifies.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   898
        if (_concurrent_iteration_count > 0) return true;
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   899
        _active_array->remove(block);
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   900
      }
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   901
      // Remove block from _allocation_list and delete it.
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   902
      _allocation_list.unlink(*block);
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   903
      // Be safepoint-polite while deleting and looping.
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 52421
diff changeset
   904
      MutexUnlocker ul(_allocation_mutex, Mutex::_no_safepoint_check_flag);
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   905
      delete_empty_block(*block);
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   906
      ThreadBlockInVM tbiv(JavaThread::current());
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   907
    }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   908
  }
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   909
  // Exceeded work limit or can't delete last block.  This will
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   910
  // cause the service thread to loop, giving other subtasks an
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   911
  // opportunity to run too.  There's no need for a notification,
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   912
  // because we are part of the service thread (unless gtesting).
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   913
  record_needs_cleanup();
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   914
  return true;
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   915
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   916
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   917
OopStorage::EntryStatus OopStorage::allocation_status(const oop* ptr) const {
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   918
  const Block* block = find_block_or_null(ptr);
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   919
  if (block != NULL) {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   920
    // Prevent block deletion and _active_array modification.
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 52421
diff changeset
   921
    MutexLocker ml(_allocation_mutex, Mutex::_no_safepoint_check_flag);
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   922
    // Block could be a false positive, so get index carefully.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   923
    size_t index = Block::active_index_safe(block);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   924
    if ((index < _active_array->block_count()) &&
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   925
        (block == _active_array->at(index)) &&
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   926
        block->contains(ptr)) {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   927
      if ((block->allocated_bitmask() & block->bitmask_for_entry(ptr)) != 0) {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   928
        return ALLOCATED_ENTRY;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   929
      } else {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   930
        return UNALLOCATED_ENTRY;
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   931
      }
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   932
    }
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   933
  }
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
   934
  return INVALID_ENTRY;
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   935
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   936
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   937
size_t OopStorage::allocation_count() const {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   938
  return _allocation_count;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   939
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   940
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   941
size_t OopStorage::block_count() const {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   942
  WithActiveArray wab(this);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   943
  // Count access is racy, but don't care.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   944
  return wab.active_array().block_count();
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   945
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   946
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   947
size_t OopStorage::total_memory_usage() const {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   948
  size_t total_size = sizeof(OopStorage);
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   949
  total_size += strlen(name()) + 1;
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   950
  total_size += sizeof(ActiveArray);
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   951
  WithActiveArray wab(this);
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   952
  const ActiveArray& blocks = wab.active_array();
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   953
  // Count access is racy, but don't care.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   954
  total_size += blocks.block_count() * Block::allocation_size();
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   955
  total_size += blocks.size() * sizeof(Block*);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   956
  return total_size;
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   957
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   958
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   959
// Parallel iteration support
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   960
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   961
uint OopStorage::BasicParState::default_estimated_thread_count(bool concurrent) {
50298
188a87cbfac3 8203843: BasicParState::default_estimated_thread_count(false) can return 0 in gtest
kbarrett
parents: 50209
diff changeset
   962
  uint configured = concurrent ? ConcGCThreads : ParallelGCThreads;
188a87cbfac3 8203843: BasicParState::default_estimated_thread_count(false) can return 0 in gtest
kbarrett
parents: 50209
diff changeset
   963
  return MAX2(1u, configured);  // Never estimate zero threads.
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   964
}
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   965
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   966
OopStorage::BasicParState::BasicParState(const OopStorage* storage,
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   967
                                         uint estimated_thread_count,
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   968
                                         bool concurrent) :
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   969
  _storage(storage),
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   970
  _active_array(_storage->obtain_active_array()),
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   971
  _block_count(0),              // initialized properly below
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   972
  _next_block(0),
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   973
  _estimated_thread_count(estimated_thread_count),
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   974
  _concurrent(concurrent)
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   975
{
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   976
  assert(estimated_thread_count > 0, "estimated thread count must be positive");
52037
d2a6c3cbc110 8211718: Supporting multiple concurrent OopStorage iterators
eosterlund
parents: 51752
diff changeset
   977
  update_concurrent_iteration_count(1);
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   978
  // Get the block count *after* iteration state updated, so concurrent
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   979
  // empty block deletion is suppressed and can't reduce the count.  But
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   980
  // ensure the count we use was written after the block with that count
50209
2fdce199fcb9 8202863: Rename OopStorage inner collection classes
kbarrett
parents: 49977
diff changeset
   981
  // was fully initialized; see ActiveArray::push.
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   982
  _block_count = _active_array->block_count_acquire();
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   983
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   984
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   985
OopStorage::BasicParState::~BasicParState() {
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
   986
  _storage->relinquish_block_array(_active_array);
52037
d2a6c3cbc110 8211718: Supporting multiple concurrent OopStorage iterators
eosterlund
parents: 51752
diff changeset
   987
  update_concurrent_iteration_count(-1);
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   988
  if (_concurrent) {
55569
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   989
    // We may have deferred some cleanup work.
8e3a0ebf3497 8226366: Excessive ServiceThread wakeups for OopStorage cleanup
kbarrett
parents: 54663
diff changeset
   990
    const_cast<OopStorage*>(_storage)->record_needs_cleanup();
52421
3021c1ad958b 8210986: Add OopStorage cleanup to ServiceThread
kbarrett
parents: 52037
diff changeset
   991
  }
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   992
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   993
52037
d2a6c3cbc110 8211718: Supporting multiple concurrent OopStorage iterators
eosterlund
parents: 51752
diff changeset
   994
void OopStorage::BasicParState::update_concurrent_iteration_count(int value) {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   995
  if (_concurrent) {
54623
1126f0607c70 8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents: 52421
diff changeset
   996
    MutexLocker ml(_storage->_active_mutex, Mutex::_no_safepoint_check_flag);
52037
d2a6c3cbc110 8211718: Supporting multiple concurrent OopStorage iterators
eosterlund
parents: 51752
diff changeset
   997
    _storage->_concurrent_iteration_count += value;
d2a6c3cbc110 8211718: Supporting multiple concurrent OopStorage iterators
eosterlund
parents: 51752
diff changeset
   998
    assert(_storage->_concurrent_iteration_count >= 0, "invariant");
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
   999
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1000
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1001
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1002
bool OopStorage::BasicParState::claim_next_segment(IterationData* data) {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1003
  data->_processed += data->_segment_end - data->_segment_start;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1004
  size_t start = OrderAccess::load_acquire(&_next_block);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1005
  if (start >= _block_count) {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1006
    return finish_iteration(data); // No more blocks available.
49333
489f1dd40582 8196876: OopStorage::assert_at_safepoint clashes with assert_at_safepoint macros in g1CollectedHeap.hpp
kbarrett
parents: 48886
diff changeset
  1007
  }
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1008
  // Try to claim several at a time, but not *too* many.  We want to
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1009
  // avoid deciding there are many available and selecting a large
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1010
  // quantity, get delayed, and then end up claiming most or all of
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1011
  // the remaining largish amount of work, leaving nothing for other
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1012
  // threads to do.  But too small a step can lead to contention
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1013
  // over _next_block, esp. when the work per block is small.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1014
  size_t max_step = 10;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1015
  size_t remaining = _block_count - start;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1016
  size_t step = MIN2(max_step, 1 + (remaining / _estimated_thread_count));
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1017
  // Atomic::add with possible overshoot.  This can perform better
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1018
  // than a CAS loop on some platforms when there is contention.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1019
  // We can cope with the uncertainty by recomputing start/end from
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1020
  // the result of the add, and dealing with potential overshoot.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1021
  size_t end = Atomic::add(step, &_next_block);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1022
  // _next_block may have changed, so recompute start from result of add.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1023
  start = end - step;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1024
  // _next_block may have changed so much that end has overshot.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1025
  end = MIN2(end, _block_count);
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1026
  // _next_block may have changed so much that even start has overshot.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1027
  if (start < _block_count) {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1028
    // Record claimed segment for iteration.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1029
    data->_segment_start = start;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1030
    data->_segment_end = end;
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1031
    return true;                // Success.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1032
  } else {
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1033
    // No more blocks to claim.
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1034
    return finish_iteration(data);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1035
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1036
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1037
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1038
bool OopStorage::BasicParState::finish_iteration(const IterationData* data) const {
51752
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
  1039
  log_info(oopstorage, blocks, stats)
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
  1040
          ("Parallel iteration on %s: blocks = " SIZE_FORMAT
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
  1041
           ", processed = " SIZE_FORMAT " (%2.f%%)",
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
  1042
           _storage->name(), _block_count, data->_processed,
43323ced5e40 8210724: Change the verbosity threshold of logging for [oopstorage,ref]
jcbeyler
parents: 51511
diff changeset
  1043
           percent_of(data->_processed, _block_count));
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1044
  return false;
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1045
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1046
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1047
const char* OopStorage::name() const { return _name; }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1048
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1049
#ifndef PRODUCT
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1050
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1051
void OopStorage::print_on(outputStream* st) const {
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1052
  size_t allocations = _allocation_count;
49977
9f758f0bb058 8200557: OopStorage parallel iteration scales poorly
kbarrett
parents: 49711
diff changeset
  1053
  size_t blocks = _active_array->block_count();
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1054
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1055
  double data_size = section_size * section_count;
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
  1056
  double alloc_percentage = percent_of((double)allocations, blocks * data_size);
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1057
48886
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
  1058
  st->print("%s: " SIZE_FORMAT " entries in " SIZE_FORMAT " blocks (%.F%%), " SIZE_FORMAT " bytes",
e1d09bd56d2d 8196083: Avoid locking in OopStorage::release
kbarrett
parents: 48816
diff changeset
  1059
            name(), allocations, blocks, alloc_percentage, total_memory_usage());
52037
d2a6c3cbc110 8211718: Supporting multiple concurrent OopStorage iterators
eosterlund
parents: 51752
diff changeset
  1060
  if (_concurrent_iteration_count > 0) {
48787
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1061
    st->print(", concurrent iteration active");
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1062
  }
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1063
}
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1064
7638bf98a312 8194312: Support parallel and concurrent JNI global handle processing
kbarrett
parents:
diff changeset
  1065
#endif // !PRODUCT