1 /*
2 * Copyright (c) 2019, 2021, 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 "memory/universe.hpp"
29 #include "runtime/interfaceSupport.inline.hpp"
30 #include "runtime/java.hpp"
31 #include "runtime/javaCalls.hpp"
32 #include "runtime/notificationThread.hpp"
33 #include "services/diagnosticArgument.hpp"
34 #include "services/diagnosticFramework.hpp"
35 #include "services/gcNotifier.hpp"
36 #include "services/lowMemoryDetector.hpp"
37
38 NotificationThread* NotificationThread::_instance = NULL;
39
initialize()40 void NotificationThread::initialize() {
41 EXCEPTION_MARK;
42
43 const char* name = "Notification Thread";
44 Handle string = java_lang_String::create_from_str(name, CHECK);
45
46 // Initialize thread_oop to put it into the system threadGroup
47 Handle thread_group (THREAD, Universe::system_thread_group());
48 Handle thread_oop = JavaCalls::construct_new_instance(
49 vmClasses::Thread_klass(),
50 vmSymbols::threadgroup_string_void_signature(),
51 thread_group,
52 string,
53 CHECK);
54
55 Klass* group = vmClasses::ThreadGroup_klass();
56 JavaValue result(T_VOID);
57 JavaCalls::call_special(&result,
58 thread_group,
59 group,
60 vmSymbols::add_method_name(),
61 vmSymbols::thread_void_signature(),
62 thread_oop,
63 THREAD);
64 {
65 MutexLocker mu(THREAD, Threads_lock);
66 NotificationThread* thread = new NotificationThread(¬ification_thread_entry);
67
68 // At this point it may be possible that no osthread was created for the
69 // JavaThread due to lack of memory. We would have to throw an exception
70 // in that case. However, since this must work and we do not allow
71 // exceptions anyway, check and abort if this fails.
72 if (thread == NULL || thread->osthread() == NULL) {
73 vm_exit_during_initialization("java.lang.OutOfMemoryError",
74 os::native_thread_creation_failed_msg());
75 }
76
77 java_lang_Thread::set_thread(thread_oop(), thread);
78 java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
79 java_lang_Thread::set_daemon(thread_oop());
80 thread->set_threadObj(thread_oop());
81 _instance = thread;
82
83 Threads::add(thread);
84 Thread::start(thread);
85 }
86 }
87
88
89
notification_thread_entry(JavaThread * jt,TRAPS)90 void NotificationThread::notification_thread_entry(JavaThread* jt, TRAPS) {
91 while (true) {
92 bool sensors_changed = false;
93 bool has_dcmd_notification_event = false;
94 bool has_gc_notification_event = false;
95 {
96 // Need state transition ThreadBlockInVM so that this thread
97 // will be handled by safepoint correctly when this thread is
98 // notified at a safepoint.
99
100 ThreadBlockInVM tbivm(jt);
101
102 MonitorLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);
103 // Process all available work on each (outer) iteration, rather than
104 // only the first recognized bit of work, to avoid frequently true early
105 // tests from potentially starving later work. Hence the use of
106 // arithmetic-or to combine results; we don't want short-circuiting.
107 while (((sensors_changed = LowMemoryDetector::has_pending_requests()) |
108 (has_dcmd_notification_event = DCmdFactory::has_pending_jmx_notification()) |
109 (has_gc_notification_event = GCNotifier::has_event()))
110 == 0) {
111 // Wait until notified that there is some work to do.
112 ml.wait(0);
113 }
114
115 }
116
117 if (sensors_changed) {
118 LowMemoryDetector::process_sensor_changes(jt);
119 }
120
121 if(has_gc_notification_event) {
122 GCNotifier::sendNotification(CHECK);
123 }
124
125 if(has_dcmd_notification_event) {
126 DCmdFactory::send_notification(CHECK);
127 }
128
129 }
130 }
131
132