Skip to content

Commit ad6611a

Browse files
committed
8371347: Move the ObjectMonitorTable to a separate new file
Reviewed-by: dholmes, coleenp
1 parent 895232f commit ad6611a

File tree

3 files changed

+386
-285
lines changed

3 files changed

+386
-285
lines changed
Lines changed: 308 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,308 @@
1+
/*
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
#include "logging/log.hpp"
26+
#include "runtime/interfaceSupport.inline.hpp"
27+
#include "runtime/javaThread.hpp"
28+
#include "runtime/mutexLocker.hpp"
29+
#include "runtime/objectMonitorTable.hpp"
30+
#include "runtime/safepoint.hpp"
31+
#include "runtime/thread.hpp"
32+
#include "runtime/timerTrace.hpp"
33+
#include "runtime/trimNativeHeap.hpp"
34+
#include "utilities/concurrentHashTableTasks.inline.hpp"
35+
#include "utilities/globalDefinitions.hpp"
36+
37+
// -----------------------------------------------------------------------------
38+
// ConcurrentHashTable storing links from objects to ObjectMonitors
39+
40+
using ConcurrentTable = ConcurrentHashTable<ObjectMonitorTableConfig, mtObjectMonitor>;
41+
42+
static ConcurrentTable* _table = nullptr;
43+
static volatile size_t _items_count = 0;
44+
static size_t _table_size = 0;
45+
static volatile bool _resize = false;
46+
47+
class ObjectMonitorTableConfig : public AllStatic {
48+
public:
49+
using Value = ObjectMonitor*;
50+
static uintx get_hash(Value const& value, bool* is_dead) {
51+
return (uintx)value->hash();
52+
}
53+
static void* allocate_node(void* context, size_t size, Value const& value) {
54+
ObjectMonitorTable::inc_items_count();
55+
return AllocateHeap(size, mtObjectMonitor);
56+
};
57+
static void free_node(void* context, void* memory, Value const& value) {
58+
ObjectMonitorTable::dec_items_count();
59+
FreeHeap(memory);
60+
}
61+
};
62+
63+
class Lookup : public StackObj {
64+
oop _obj;
65+
66+
public:
67+
explicit Lookup(oop obj) : _obj(obj) {}
68+
69+
uintx get_hash() const {
70+
uintx hash = _obj->mark().hash();
71+
assert(hash != 0, "should have a hash");
72+
return hash;
73+
}
74+
75+
bool equals(ObjectMonitor** value) {
76+
assert(*value != nullptr, "must be");
77+
return (*value)->object_refers_to(_obj);
78+
}
79+
80+
bool is_dead(ObjectMonitor** value) {
81+
assert(*value != nullptr, "must be");
82+
return false;
83+
}
84+
};
85+
86+
class LookupMonitor : public StackObj {
87+
ObjectMonitor* _monitor;
88+
89+
public:
90+
explicit LookupMonitor(ObjectMonitor* monitor) : _monitor(monitor) {}
91+
92+
uintx get_hash() const {
93+
return _monitor->hash();
94+
}
95+
96+
bool equals(ObjectMonitor** value) {
97+
return (*value) == _monitor;
98+
}
99+
100+
bool is_dead(ObjectMonitor** value) {
101+
assert(*value != nullptr, "must be");
102+
return (*value)->object_is_dead();
103+
}
104+
};
105+
106+
void ObjectMonitorTable::inc_items_count() {
107+
AtomicAccess::inc(&_items_count, memory_order_relaxed);
108+
}
109+
110+
void ObjectMonitorTable::dec_items_count() {
111+
AtomicAccess::dec(&_items_count, memory_order_relaxed);
112+
}
113+
114+
double ObjectMonitorTable::get_load_factor() {
115+
size_t count = AtomicAccess::load(&_items_count);
116+
return (double)count / (double)_table_size;
117+
}
118+
119+
size_t ObjectMonitorTable::table_size(Thread* current) {
120+
return ((size_t)1) << _table->get_size_log2(current);
121+
}
122+
123+
size_t ObjectMonitorTable::max_log_size() {
124+
// TODO[OMTable]: Evaluate the max size.
125+
// TODO[OMTable]: Need to fix init order to use Universe::heap()->max_capacity();
126+
// Using MaxHeapSize directly this early may be wrong, and there
127+
// are definitely rounding errors (alignment).
128+
const size_t max_capacity = MaxHeapSize;
129+
const size_t min_object_size = CollectedHeap::min_dummy_object_size() * HeapWordSize;
130+
const size_t max_objects = max_capacity / MAX2(MinObjAlignmentInBytes, checked_cast<int>(min_object_size));
131+
const size_t log_max_objects = log2i_graceful(max_objects);
132+
133+
return MAX2(MIN2<size_t>(SIZE_BIG_LOG2, log_max_objects), min_log_size());
134+
}
135+
136+
// ~= log(AvgMonitorsPerThreadEstimate default)
137+
size_t ObjectMonitorTable::min_log_size() {
138+
return 10;
139+
}
140+
141+
template<typename V>
142+
size_t ObjectMonitorTable::clamp_log_size(V log_size) {
143+
return MAX2(MIN2(log_size, checked_cast<V>(max_log_size())), checked_cast<V>(min_log_size()));
144+
}
145+
146+
size_t ObjectMonitorTable::initial_log_size() {
147+
const size_t estimate = log2i(MAX2(os::processor_count(), 1)) + log2i(MAX2(AvgMonitorsPerThreadEstimate, size_t(1)));
148+
return clamp_log_size(estimate);
149+
}
150+
151+
size_t ObjectMonitorTable::grow_hint() {
152+
return ConcurrentTable::DEFAULT_GROW_HINT;
153+
}
154+
155+
void ObjectMonitorTable::create() {
156+
_table = new ConcurrentTable(initial_log_size(), max_log_size(), grow_hint());
157+
_items_count = 0;
158+
_table_size = table_size(Thread::current());
159+
_resize = false;
160+
}
161+
162+
void ObjectMonitorTable::verify_monitor_get_result(oop obj, ObjectMonitor* monitor) {
163+
#ifdef ASSERT
164+
if (SafepointSynchronize::is_at_safepoint()) {
165+
bool has_monitor = obj->mark().has_monitor();
166+
assert(has_monitor == (monitor != nullptr),
167+
"Inconsistency between markWord and ObjectMonitorTable has_monitor: %s monitor: " PTR_FORMAT,
168+
BOOL_TO_STR(has_monitor), p2i(monitor));
169+
}
170+
#endif
171+
}
172+
173+
ObjectMonitor* ObjectMonitorTable::monitor_get(Thread* current, oop obj) {
174+
ObjectMonitor* result = nullptr;
175+
Lookup lookup_f(obj);
176+
auto found_f = [&](ObjectMonitor** found) {
177+
assert((*found)->object_peek() == obj, "must be");
178+
result = *found;
179+
};
180+
_table->get(current, lookup_f, found_f);
181+
verify_monitor_get_result(obj, result);
182+
return result;
183+
}
184+
185+
void ObjectMonitorTable::try_notify_grow() {
186+
if (!_table->is_max_size_reached() && !AtomicAccess::load(&_resize)) {
187+
AtomicAccess::store(&_resize, true);
188+
if (Service_lock->try_lock()) {
189+
Service_lock->notify();
190+
Service_lock->unlock();
191+
}
192+
}
193+
}
194+
195+
bool ObjectMonitorTable::should_grow() {
196+
return get_load_factor() > GROW_LOAD_FACTOR && !_table->is_max_size_reached();
197+
}
198+
199+
bool ObjectMonitorTable::should_resize() {
200+
return should_grow() || should_shrink() || AtomicAccess::load(&_resize);
201+
}
202+
203+
template <typename Task, typename... Args>
204+
bool ObjectMonitorTable::run_task(JavaThread* current, Task& task, const char* task_name, Args&... args) {
205+
if (task.prepare(current)) {
206+
log_trace(monitortable)("Started to %s", task_name);
207+
TraceTime timer(task_name, TRACETIME_LOG(Debug, monitortable, perf));
208+
while (task.do_task(current, args...)) {
209+
task.pause(current);
210+
{
211+
ThreadBlockInVM tbivm(current);
212+
}
213+
task.cont(current);
214+
}
215+
task.done(current);
216+
return true;
217+
}
218+
return false;
219+
}
220+
221+
bool ObjectMonitorTable::grow(JavaThread* current) {
222+
ConcurrentTable::GrowTask grow_task(_table);
223+
if (run_task(current, grow_task, "Grow")) {
224+
_table_size = table_size(current);
225+
log_info(monitortable)("Grown to size: %zu", _table_size);
226+
return true;
227+
}
228+
return false;
229+
}
230+
231+
bool ObjectMonitorTable::clean(JavaThread* current) {
232+
ConcurrentTable::BulkDeleteTask clean_task(_table);
233+
auto is_dead = [&](ObjectMonitor** monitor) {
234+
return (*monitor)->object_is_dead();
235+
};
236+
auto do_nothing = [&](ObjectMonitor** monitor) {};
237+
NativeHeapTrimmer::SuspendMark sm("ObjectMonitorTable");
238+
return run_task(current, clean_task, "Clean", is_dead, do_nothing);
239+
}
240+
241+
bool ObjectMonitorTable::resize(JavaThread* current) {
242+
LogTarget(Info, monitortable) lt;
243+
bool success = false;
244+
245+
if (should_grow()) {
246+
lt.print("Start growing with load factor %f", get_load_factor());
247+
success = grow(current);
248+
} else {
249+
if (!_table->is_max_size_reached() && AtomicAccess::load(&_resize)) {
250+
lt.print("WARNING: Getting resize hints with load factor %f", get_load_factor());
251+
}
252+
lt.print("Start cleaning with load factor %f", get_load_factor());
253+
success = clean(current);
254+
}
255+
256+
AtomicAccess::store(&_resize, false);
257+
258+
return success;
259+
}
260+
261+
ObjectMonitor* ObjectMonitorTable::monitor_put_get(Thread* current, ObjectMonitor* monitor, oop obj) {
262+
// Enter the monitor into the concurrent hashtable.
263+
ObjectMonitor* result = monitor;
264+
Lookup lookup_f(obj);
265+
auto found_f = [&](ObjectMonitor** found) {
266+
assert((*found)->object_peek() == obj, "must be");
267+
result = *found;
268+
};
269+
bool grow;
270+
_table->insert_get(current, lookup_f, monitor, found_f, &grow);
271+
verify_monitor_get_result(obj, result);
272+
if (grow) {
273+
try_notify_grow();
274+
}
275+
return result;
276+
}
277+
278+
bool ObjectMonitorTable::remove_monitor_entry(Thread* current, ObjectMonitor* monitor) {
279+
LookupMonitor lookup_f(monitor);
280+
return _table->remove(current, lookup_f);
281+
}
282+
283+
bool ObjectMonitorTable::contains_monitor(Thread* current, ObjectMonitor* monitor) {
284+
LookupMonitor lookup_f(monitor);
285+
bool result = false;
286+
auto found_f = [&](ObjectMonitor** found) {
287+
result = true;
288+
};
289+
_table->get(current, lookup_f, found_f);
290+
return result;
291+
}
292+
293+
void ObjectMonitorTable::print_on(outputStream* st) {
294+
auto printer = [&] (ObjectMonitor** entry) {
295+
ObjectMonitor* om = *entry;
296+
oop obj = om->object_peek();
297+
st->print("monitor=" PTR_FORMAT ", ", p2i(om));
298+
st->print("object=" PTR_FORMAT, p2i(obj));
299+
assert(obj->mark().hash() == om->hash(), "hash must match");
300+
st->cr();
301+
return true;
302+
};
303+
if (SafepointSynchronize::is_at_safepoint()) {
304+
_table->do_safepoint_scan(printer);
305+
} else {
306+
_table->do_scan(Thread::current(), printer);
307+
}
308+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright (c) 2024, 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*
23+
*/
24+
25+
#ifndef SHARE_RUNTIME_OBJECTMONITORTABLE_HPP
26+
#define SHARE_RUNTIME_OBJECTMONITORTABLE_HPP
27+
28+
#include "memory/allStatic.hpp"
29+
#include "oops/oopsHierarchy.hpp"
30+
#include "utilities/globalDefinitions.hpp"
31+
32+
class JavaThread;
33+
class ObjectMonitor;
34+
class ObjectMonitorTableConfig;
35+
class outputStream;
36+
class Thread;
37+
38+
class ObjectMonitorTable : AllStatic {
39+
friend class ObjectMonitorTableConfig;
40+
41+
private:
42+
static void inc_items_count();
43+
static void dec_items_count();
44+
static double get_load_factor();
45+
static size_t table_size(Thread* current);
46+
static size_t max_log_size();
47+
static size_t min_log_size();
48+
49+
template <typename V>
50+
static size_t clamp_log_size(V log_size);
51+
static size_t initial_log_size();
52+
static size_t grow_hint();
53+
54+
public:
55+
static void create();
56+
static void verify_monitor_get_result(oop obj, ObjectMonitor* monitor);
57+
static ObjectMonitor* monitor_get(Thread* current, oop obj);
58+
static void try_notify_grow();
59+
static bool should_shrink() { return false; } // Not implemented
60+
61+
static constexpr double GROW_LOAD_FACTOR = 0.75;
62+
63+
static bool should_grow();
64+
static bool should_resize();
65+
66+
template <typename Task, typename... Args>
67+
static bool run_task(JavaThread* current, Task& task, const char* task_name, Args&... args);
68+
static bool grow(JavaThread* current);
69+
static bool clean(JavaThread* current);
70+
static bool resize(JavaThread* current);
71+
static ObjectMonitor* monitor_put_get(Thread* current, ObjectMonitor* monitor, oop obj);
72+
static bool remove_monitor_entry(Thread* current, ObjectMonitor* monitor);
73+
static bool contains_monitor(Thread* current, ObjectMonitor* monitor);
74+
static void print_on(outputStream* st);
75+
};
76+
77+
#endif // SHARE_RUNTIME_OBJECTMONITORTABLE_HPP

0 commit comments

Comments
 (0)