jdk
1/*
2* Copyright (c) 2020, 2024, 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 "precompiled.hpp"26#include "classfile/javaClasses.hpp"27#include "classfile/vmClasses.hpp"28#include "classfile/vmSymbols.hpp"29#include "logging/log.hpp"30#include "logging/logStream.hpp"31#include "memory/universe.hpp"32#include "runtime/interfaceSupport.inline.hpp"33#include "runtime/java.hpp"34#include "runtime/javaCalls.hpp"35#include "runtime/monitorDeflationThread.hpp"36#include "runtime/mutexLocker.hpp"37#include "runtime/synchronizer.hpp"38
39void MonitorDeflationThread::initialize() {40EXCEPTION_MARK;41
42const char* name = "Monitor Deflation Thread";43Handle thread_oop = JavaThread::create_system_thread_object(name, CHECK);44
45MonitorDeflationThread* thread = new MonitorDeflationThread(&monitor_deflation_thread_entry);46JavaThread::vm_exit_on_osthread_failure(thread);47
48JavaThread::start_internal_daemon(THREAD, thread, thread_oop, NearMaxPriority);49}
50
51void MonitorDeflationThread::monitor_deflation_thread_entry(JavaThread* jt, TRAPS) {52
53// We wait for the lowest of these two intervals:54// - AsyncDeflationInterval55// Normal threshold-based deflation heuristic checks the conditions at this interval.56// See is_async_deflation_needed().57// - GuaranteedAsyncDeflationInterval58// Backup deflation heuristic checks the conditions at this interval.59// See is_async_deflation_needed().60//61intx wait_time = max_intx;62if (AsyncDeflationInterval > 0) {63wait_time = MIN2(wait_time, AsyncDeflationInterval);64}65if (GuaranteedAsyncDeflationInterval > 0) {66wait_time = MIN2(wait_time, GuaranteedAsyncDeflationInterval);67}68
69// If all options are disabled, then wait time is not defined, and the deflation70// is effectively disabled. In that case, exit the thread immediately after printing71// a warning message.72if (wait_time == max_intx) {73warning("Async deflation is disabled");74return;75}76
77while (true) {78{79// Need state transition ThreadBlockInVM so that this thread80// will be handled by safepoint correctly when this thread is81// notified at a safepoint.82
83ThreadBlockInVM tbivm(jt);84
85MonitorLocker ml(MonitorDeflation_lock, Mutex::_no_safepoint_check_flag);86while (!ObjectSynchronizer::is_async_deflation_needed()) {87// Wait until notified that there is some work to do.88ml.wait(wait_time);89}90}91
92(void)ObjectSynchronizer::deflate_idle_monitors();93
94if (log_is_enabled(Debug, monitorinflation)) {95// The VMThread calls do_final_audit_and_print_stats() which calls96// audit_and_print_stats() at the Info level at VM exit time.97LogStreamHandle(Debug, monitorinflation) ls;98ObjectSynchronizer::audit_and_print_stats(&ls, false /* on_exit */);99}100}101}
102