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