src/hotspot/share/gc/shared/ptrQueue.cpp
author kbarrett
Wed, 26 Jun 2019 13:18:38 -0400
changeset 55498 e64383344f14
parent 54970 76d3d96a8bc2
child 58059 baa4dd528de0
permissions -rw-r--r--
8225255: Make SATB qset lock-free Summary: Refactor PtrQueueSet, use lock-free stack for SATB completed buffers Reviewed-by: tschatzl, shade
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     1
/*
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
     2
 * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     4
 *
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     7
 * published by the Free Software Foundation.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     8
 *
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    13
 * accompanied this code).
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    14
 *
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5082
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5082
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5082
diff changeset
    21
 * questions.
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    22
 *
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    23
 */
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
51441
2e91d927e00c 8154343: Make SATB related code available to other GCs
kbarrett
parents: 51332
diff changeset
    26
#include "gc/shared/ptrQueue.hpp"
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
    27
#include "logging/log.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/allocation.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "memory/allocation.inline.hpp"
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
    30
#include "runtime/atomic.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
#include "runtime/mutex.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    32
#include "runtime/mutexLocker.hpp"
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
    33
#include "runtime/orderAccess.hpp"
14583
d70ee55535f4 8003935: Simplify the needed includes for using Thread::current()
stefank
parents: 13963
diff changeset
    34
#include "runtime/thread.inline.hpp"
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
    35
#include "utilities/globalCounter.inline.hpp"
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    36
36354
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
    37
#include <new>
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
    38
54255
c81fbf340ceb 8220240: Refactor shared dirty card queue
kbarrett
parents: 53404
diff changeset
    39
PtrQueue::PtrQueue(PtrQueueSet* qset, bool active) :
46443
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    40
  _qset(qset),
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    41
  _active(active),
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    42
  _index(0),
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    43
  _capacity_in_bytes(0),
54255
c81fbf340ceb 8220240: Refactor shared dirty card queue
kbarrett
parents: 53404
diff changeset
    44
  _buf(NULL)
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    45
{}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    46
28507
354ef83ee258 8048949: Requeue queue implementation
tschatzl
parents: 27880
diff changeset
    47
PtrQueue::~PtrQueue() {
54255
c81fbf340ceb 8220240: Refactor shared dirty card queue
kbarrett
parents: 53404
diff changeset
    48
  assert(_buf == NULL, "queue must be flushed before delete");
28507
354ef83ee258 8048949: Requeue queue implementation
tschatzl
parents: 27880
diff changeset
    49
}
354ef83ee258 8048949: Requeue queue implementation
tschatzl
parents: 27880
diff changeset
    50
354ef83ee258 8048949: Requeue queue implementation
tschatzl
parents: 27880
diff changeset
    51
void PtrQueue::flush_impl() {
46305
bff6d23aa1e3 8175221: Cleanup DirtyCardQueueSet::concatenate_log
kbarrett
parents: 37065
diff changeset
    52
  if (_buf != NULL) {
46443
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    53
    BufferNode* node = BufferNode::make_node_from_buffer(_buf, index());
37065
c00d1c2ffb7c 8150676: Use BufferNode index
kbarrett
parents: 36371
diff changeset
    54
    if (is_empty()) {
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    55
      // No work to do.
37065
c00d1c2ffb7c 8150676: Use BufferNode index
kbarrett
parents: 36371
diff changeset
    56
      qset()->deallocate_buffer(node);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    57
    } else {
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
    58
      qset()->enqueue_completed_buffer(node);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    59
    }
1560
1b328492b7f8 6770608: G1: Mutator thread can flush barrier and satb queues during safepoint
iveresov
parents: 1374
diff changeset
    60
    _buf = NULL;
46443
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    61
    set_index(0);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    62
  }
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    63
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    64
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    65
void PtrQueue::enqueue_known_active(void* ptr) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    66
  while (_index == 0) {
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    67
    handle_zero_index();
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    68
  }
4481
de92ec484f5e 6862387: tune concurrent refinement further
iveresov
parents: 4454
diff changeset
    69
46443
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    70
  assert(_buf != NULL, "postcondition");
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    71
  assert(index() > 0, "postcondition");
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    72
  assert(index() <= capacity(), "invariant");
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    73
  _index -= _element_size;
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
    74
  _buf[index()] = ptr;
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    75
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
    76
54970
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    77
void PtrQueue::handle_zero_index() {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    78
  assert(index() == 0, "precondition");
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    79
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    80
  if (_buf != NULL) {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    81
    handle_completed_buffer();
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    82
  } else {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    83
    // Bootstrapping kludge; lazily initialize capacity.  The initial
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    84
    // thread's queues are constructed before the second phase of the
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    85
    // two-phase initialization of the associated qsets.  As a result,
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    86
    // we can't initialize _capacity_in_bytes in the queue constructor.
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    87
    if (_capacity_in_bytes == 0) {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    88
      _capacity_in_bytes = index_to_byte_index(qset()->buffer_size());
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    89
    }
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    90
    allocate_buffer();
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    91
  }
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    92
}
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    93
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    94
void PtrQueue::allocate_buffer() {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    95
  _buf = qset()->allocate_buffer();
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    96
  reset();
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    97
}
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    98
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
    99
void PtrQueue::enqueue_completed_buffer() {
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   100
  assert(_buf != NULL, "precondition");
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   101
  BufferNode* node = BufferNode::make_node_from_buffer(_buf, index());
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   102
  qset()->enqueue_completed_buffer(node);
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   103
  allocate_buffer();
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   104
}
76d3d96a8bc2 8224167: Refactor PtrQueue completed buffer processing
kbarrett
parents: 54810
diff changeset
   105
46443
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
   106
BufferNode* BufferNode::allocate(size_t size) {
cdb638b5ec53 8178836: Improve PtrQueue index abstraction
kbarrett
parents: 46305
diff changeset
   107
  size_t byte_size = size * sizeof(void*);
36354
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   108
  void* data = NEW_C_HEAP_ARRAY(char, buffer_offset() + byte_size, mtGC);
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   109
  return new (data) BufferNode;
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   110
}
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   111
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   112
void BufferNode::deallocate(BufferNode* node) {
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   113
  node->~BufferNode();
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   114
  FREE_C_HEAP_ARRAY(char, node);
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   115
}
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   116
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   117
BufferNode::Allocator::Allocator(const char* name, size_t buffer_size) :
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   118
  _buffer_size(buffer_size),
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   119
  _pending_list(),
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   120
  _free_list(),
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   121
  _pending_count(0),
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   122
  _free_count(0),
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   123
  _transfer_lock(false)
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   124
{
54810
258170da6d3a 8223186: HotSpot compile warnings from GCC 9
ysuenaga
parents: 54623
diff changeset
   125
  strncpy(_name, name, sizeof(_name) - 1);
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   126
  _name[sizeof(_name) - 1] = '\0';
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   127
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   128
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   129
BufferNode::Allocator::~Allocator() {
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   130
  delete_list(_free_list.pop_all());
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   131
  delete_list(_pending_list.pop_all());
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   132
}
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   133
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   134
void BufferNode::Allocator::delete_list(BufferNode* list) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   135
  while (list != NULL) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   136
    BufferNode* next = list->next();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   137
    DEBUG_ONLY(list->set_next(NULL);)
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   138
    BufferNode::deallocate(list);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   139
    list = next;
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   140
  }
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   141
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   142
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   143
size_t BufferNode::Allocator::free_count() const {
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   144
  return Atomic::load(&_free_count);
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   145
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   146
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   147
BufferNode* BufferNode::Allocator::allocate() {
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   148
  BufferNode* node;
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   149
  {
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   150
    // Protect against ABA; see release().
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   151
    GlobalCounter::CriticalSection cs(Thread::current());
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   152
    node = _free_list.pop();
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   153
  }
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   154
  if (node == NULL) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   155
    node = BufferNode::allocate(_buffer_size);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   156
  } else {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   157
    // Decrement count after getting buffer from free list.  This, along
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   158
    // with incrementing count before adding to free list, ensures count
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   159
    // never underflows.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   160
    size_t count = Atomic::sub(1u, &_free_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   161
    assert((count + 1) != 0, "_free_count underflow");
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   162
  }
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   163
  return node;
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   164
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   165
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   166
// To solve the ABA problem for lock-free stack pop, allocate does the
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   167
// pop inside a critical section, and release synchronizes on the
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   168
// critical sections before adding to the _free_list.  But we don't
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   169
// want to make every release have to do a synchronize.  Instead, we
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   170
// initially place released nodes on the _pending_list, and transfer
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   171
// them to the _free_list in batches.  Only one transfer at a time is
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   172
// permitted, with a lock bit to control access to that phase.  A
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   173
// transfer takes all the nodes from the _pending_list, synchronizes on
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   174
// the _free_list pops, and then adds the former pending nodes to the
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   175
// _free_list.  While that's happening, other threads might be adding
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   176
// other nodes to the _pending_list, to be dealt with by some later
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   177
// transfer.
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   178
void BufferNode::Allocator::release(BufferNode* node) {
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   179
  assert(node != NULL, "precondition");
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   180
  assert(node->next() == NULL, "precondition");
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   181
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   182
  // Desired minimum transfer batch size.  There is relatively little
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   183
  // importance to the specific number.  It shouldn't be too big, else
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   184
  // we're wasting space when the release rate is low.  If the release
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   185
  // rate is high, we might accumulate more than this before being
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   186
  // able to start a new transfer, but that's okay.  Also note that
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   187
  // the allocation rate and the release rate are going to be fairly
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   188
  // similar, due to how the buffers are used.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   189
  const size_t trigger_transfer = 10;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   190
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   191
  // Add to pending list. Update count first so no underflow in transfer.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   192
  size_t pending_count = Atomic::add(1u, &_pending_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   193
  _pending_list.push(*node);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   194
  if (pending_count > trigger_transfer) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   195
    try_transfer_pending();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   196
  }
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   197
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   198
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   199
// Try to transfer nodes from _pending_list to _free_list, with a
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   200
// synchronization delay for any in-progress pops from the _free_list,
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   201
// to solve ABA there.  Return true if performed a (possibly empty)
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   202
// transfer, false if blocked from doing so by some other thread's
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   203
// in-progress transfer.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   204
bool BufferNode::Allocator::try_transfer_pending() {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   205
  // Attempt to claim the lock.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   206
  if (Atomic::load(&_transfer_lock) || // Skip CAS if likely to fail.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   207
      Atomic::cmpxchg(true, &_transfer_lock, false)) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   208
    return false;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   209
  }
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   210
  // Have the lock; perform the transfer.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   211
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   212
  // Claim all the pending nodes.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   213
  BufferNode* first = _pending_list.pop_all();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   214
  if (first != NULL) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   215
    // Prepare to add the claimed nodes, and update _pending_count.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   216
    BufferNode* last = first;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   217
    size_t count = 1;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   218
    for (BufferNode* next = first->next(); next != NULL; next = next->next()) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   219
      last = next;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   220
      ++count;
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   221
    }
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   222
    Atomic::sub(count, &_pending_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   223
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   224
    // Wait for any in-progress pops, to avoid ABA for them.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   225
    GlobalCounter::write_synchronize();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   226
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   227
    // Add synchronized nodes to _free_list.
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   228
    // Update count first so no underflow in allocate().
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   229
    Atomic::add(count, &_free_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   230
    _free_list.prepend(*first, *last);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   231
    log_trace(gc, ptrqueue, freelist)
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   232
             ("Transferred %s pending to free: " SIZE_FORMAT, name(), count);
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   233
  }
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   234
  OrderAccess::release_store(&_transfer_lock, false);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   235
  return true;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   236
}
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   237
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   238
size_t BufferNode::Allocator::reduce_free_list(size_t remove_goal) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   239
  try_transfer_pending();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   240
  size_t removed = 0;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   241
  for ( ; removed < remove_goal; ++removed) {
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   242
    BufferNode* node = _free_list.pop();
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   243
    if (node == NULL) break;
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   244
    BufferNode::deallocate(node);
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   245
  }
53404
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   246
  size_t new_count = Atomic::sub(removed, &_free_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   247
  log_debug(gc, ptrqueue, freelist)
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   248
           ("Reduced %s free list by " SIZE_FORMAT " to " SIZE_FORMAT,
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   249
            name(), removed, new_count);
9ff1e6cacac3 8212826: Make PtrQueue free list lock-free
kbarrett
parents: 53102
diff changeset
   250
  return removed;
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   251
}
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   252
55498
e64383344f14 8225255: Make SATB qset lock-free
kbarrett
parents: 54970
diff changeset
   253
PtrQueueSet::PtrQueueSet() :
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   254
  _allocator(NULL),
53102
35530ca3e0b2 8214201: Make PtrQueueSet completed buffer list private
kbarrett
parents: 52726
diff changeset
   255
  _all_active(false)
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   256
{}
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   257
55498
e64383344f14 8225255: Make SATB qset lock-free
kbarrett
parents: 54970
diff changeset
   258
PtrQueueSet::~PtrQueueSet() {}
33761
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   259
55498
e64383344f14 8225255: Make SATB qset lock-free
kbarrett
parents: 54970
diff changeset
   260
void PtrQueueSet::initialize(BufferNode::Allocator* allocator) {
e64383344f14 8225255: Make SATB qset lock-free
kbarrett
parents: 54970
diff changeset
   261
  assert(allocator != NULL, "Init order issue?");
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   262
  _allocator = allocator;
33761
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   263
}
329db4b51480 6899049: G1: Clean up code in ptrQueue.[ch]pp and ptrQueue.inline.hpp
kbarrett
parents: 30764
diff changeset
   264
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   265
void** PtrQueueSet::allocate_buffer() {
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   266
  BufferNode* node = _allocator->allocate();
36354
28bbe66a0498 8150419: Cleanup BufferNode API
kbarrett
parents: 33761
diff changeset
   267
  return BufferNode::make_buffer_from_node(node);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   268
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   269
37065
c00d1c2ffb7c 8150676: Use BufferNode index
kbarrett
parents: 36371
diff changeset
   270
void PtrQueueSet::deallocate_buffer(BufferNode* node) {
52582
6df094be7f58 8213352: Separate BufferNode allocation from PtrQueueSet
kbarrett
parents: 51441
diff changeset
   271
  _allocator->release(node);
1374
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   272
}
4c24294029a9 6711316: Open source the Garbage-First garbage collector
ysr
parents:
diff changeset
   273