File: test_bufferNodeAllocator.cpp

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (242 lines) | stat: -rw-r--r-- 8,258 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
 * Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "precompiled.hpp"
#include "gc/shared/bufferNode.hpp"
#include "memory/allocation.hpp"
#include "runtime/atomic.hpp"
#include "runtime/interfaceSupport.inline.hpp"
#include "runtime/semaphore.inline.hpp"
#include "runtime/thread.hpp"
#include "utilities/globalCounter.inline.hpp"
#include "utilities/globalDefinitions.hpp"
#include "utilities/ostream.hpp"
#include "threadHelper.inline.hpp"
#include "unittest.hpp"

class BufferNode::TestSupport : AllStatic {
public:
  static bool try_transfer_pending(Allocator* allocator) {
    return allocator->_free_list.try_transfer_pending();
  }

  class CompletedList;
  class AllocatorThread;
  class ProcessorThread;
};

typedef BufferNode::TestSupport::CompletedList CompletedList;
typedef BufferNode::TestSupport::AllocatorThread AllocatorThread;
typedef BufferNode::TestSupport::ProcessorThread ProcessorThread;

// Some basic testing of BufferNode::Allocator.
TEST_VM(BufferNodeAllocatorTest, test) {
  const size_t buffer_size = 256;
  BufferNode::Allocator allocator("Test Buffer Allocator", buffer_size);
  ASSERT_EQ(buffer_size, allocator.buffer_size());

  // Allocate some new nodes for use in testing.
  BufferNode* nodes[10] = {};
  const size_t node_count = ARRAY_SIZE(nodes);
  for (size_t i = 0; i < node_count; ++i) {
    ASSERT_EQ(0u, allocator.free_count());
    nodes[i] = allocator.allocate();
    ASSERT_EQ(nullptr, nodes[i]->next());
  }

  // Release the nodes, adding them to the allocator's free list.
  for (size_t i = 0; i < node_count; ++i) {
    allocator.release(nodes[i]);
  }
  ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(&allocator));
  ASSERT_EQ(node_count, allocator.free_count());

  // Allocate nodes from the free list.
  for (size_t i = 0; i < node_count; ++i) {
    size_t j = node_count - i;
    ASSERT_EQ(nodes[j - 1], allocator.allocate());
  }
  ASSERT_EQ(0u, allocator.free_count());

  // Release nodes back to the free list.
  for (size_t i = 0; i < node_count; ++i) {
    allocator.release(nodes[i]);
  }
  ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(&allocator));
  ASSERT_EQ(node_count, allocator.free_count());
}

// Stress test with lock-free allocator and completed buffer list.
// Completed buffer list pop avoids ABA by also being in a critical
// section that is synchronized by the allocator's release.

class BufferNode::TestSupport::CompletedList {
  BufferNode::Stack _completed_list;

public:
  CompletedList() : _completed_list() {}

  ~CompletedList() {
    assert(_completed_list.empty(), "completed list not empty");
  }

  void push(BufferNode* node) {
    assert(node != nullptr, "precondition");
    _completed_list.push(*node);
  }

  BufferNode* pop() {
    GlobalCounter::CriticalSection cs(Thread::current());
    return _completed_list.pop();
  }
};

// Simulate a mutator thread, allocating buffers and adding them to
// the completed buffer list.
class BufferNode::TestSupport::AllocatorThread : public JavaTestThread {
  BufferNode::Allocator* _allocator;
  CompletedList* _cbl;
  volatile size_t* _total_allocations;
  volatile bool* _continue_running;
  size_t _allocations;

public:
  AllocatorThread(Semaphore* post,
                  BufferNode::Allocator* allocator,
                  CompletedList* cbl,
                  volatile size_t* total_allocations,
                  volatile bool* continue_running) :
    JavaTestThread(post),
    _allocator(allocator),
    _cbl(cbl),
    _total_allocations(total_allocations),
    _continue_running(continue_running),
    _allocations(0)
  {}

  virtual void main_run() {
    while (Atomic::load_acquire(_continue_running)) {
      BufferNode* node = _allocator->allocate();
      _cbl->push(node);
      ++_allocations;
      ThreadBlockInVM tbiv(this); // Safepoint check.
    }
    tty->print_cr("allocations: " SIZE_FORMAT, _allocations);
    Atomic::add(_total_allocations, _allocations);
  }
};

// Simulate a GC thread, taking buffers from the completed buffer list
// and returning them to the allocator.
class BufferNode::TestSupport::ProcessorThread : public JavaTestThread {
  BufferNode::Allocator* _allocator;
  CompletedList* _cbl;
  volatile bool* _continue_running;

public:
  ProcessorThread(Semaphore* post,
                  BufferNode::Allocator* allocator,
                  CompletedList* cbl,
                  volatile bool* continue_running) :
    JavaTestThread(post),
    _allocator(allocator),
    _cbl(cbl),
    _continue_running(continue_running)
  {}

  virtual void main_run() {
    bool shutdown_requested = false;
    while (true) {
      BufferNode* node = _cbl->pop();
      if (node != nullptr) {
        _allocator->release(node);
      } else if (shutdown_requested) {
        return;
      } else if (!Atomic::load_acquire(_continue_running)) {
        // To avoid a race that could leave buffers in the list after this
        // thread has shut down, continue processing until the list is empty
        // *after* the shut down request has been received.
        shutdown_requested = true;
      }
      ThreadBlockInVM tbiv(this); // Safepoint check.
    }
  }
};

static void run_test(BufferNode::Allocator* allocator, CompletedList* cbl) {
  const uint nthreads = 4;
  const uint milliseconds_to_run = 1000;

  Semaphore post;
  volatile size_t total_allocations = 0;
  volatile bool allocator_running = true;
  volatile bool processor_running = true;

  ProcessorThread* proc_threads[nthreads] = {};
  for (uint i = 0; i < nthreads; ++i) {
    proc_threads[i] = new ProcessorThread(&post,
                                          allocator,
                                          cbl,
                                          &processor_running);
    proc_threads[i]->doit();
  }

  AllocatorThread* alloc_threads[nthreads] = {};
  for (uint i = 0; i < nthreads; ++i) {
    alloc_threads[i] = new AllocatorThread(&post,
                                           allocator,
                                           cbl,
                                           &total_allocations,
                                           &allocator_running);
    alloc_threads[i]->doit();
  }

  JavaThread* this_thread = JavaThread::current();
  tty->print_cr("Stressing allocator for %u ms", milliseconds_to_run);
  {
    ThreadInVMfromNative invm(this_thread);
    this_thread->sleep(milliseconds_to_run);
  }
  Atomic::release_store(&allocator_running, false);
  for (uint i = 0; i < nthreads; ++i) {
    ThreadInVMfromNative invm(this_thread);
    post.wait_with_safepoint_check(this_thread);
  }
  Atomic::release_store(&processor_running, false);
  for (uint i = 0; i < nthreads; ++i) {
    ThreadInVMfromNative invm(this_thread);
    post.wait_with_safepoint_check(this_thread);
  }
  ASSERT_TRUE(BufferNode::TestSupport::try_transfer_pending(allocator));
  tty->print_cr("total allocations: " SIZE_FORMAT, total_allocations);
  tty->print_cr("allocator free count: " SIZE_FORMAT, allocator->free_count());
}

const size_t buffer_size = 1024;

TEST_VM(BufferNodeAllocatorTest, stress_free_list_allocator) {
  BufferNode::Allocator allocator("Test Allocator", buffer_size);
  CompletedList completed;
  run_test(&allocator, &completed);
}