Mon, 18 Mar 2013 13:19:06 +0100
8008555: Debugging code in compiled method sometimes leaks memory
Summary: support for strings that have same life-time as code that uses them.
Reviewed-by: kvn, twisti
kamg@2511 | 1 | /* |
kamg@2511 | 2 | * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. |
kamg@2511 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
kamg@2511 | 4 | * |
kamg@2511 | 5 | * This code is free software; you can redistribute it and/or modify it |
kamg@2511 | 6 | * under the terms of the GNU General Public License version 2 only, as |
kamg@2511 | 7 | * published by the Free Software Foundation. |
kamg@2511 | 8 | * |
kamg@2511 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
kamg@2511 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
kamg@2511 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
kamg@2511 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
kamg@2511 | 13 | * accompanied this code). |
kamg@2511 | 14 | * |
kamg@2511 | 15 | * You should have received a copy of the GNU General Public License version |
kamg@2511 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
kamg@2511 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
kamg@2511 | 18 | * |
kamg@2511 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
kamg@2511 | 20 | * or visit www.oracle.com if you need additional information or have any |
kamg@2511 | 21 | * questions. |
kamg@2511 | 22 | * |
kamg@2511 | 23 | */ |
kamg@2511 | 24 | |
kamg@2511 | 25 | #include "precompiled.hpp" |
kamg@2511 | 26 | #include "runtime/interfaceSupport.hpp" |
kamg@2511 | 27 | #include "runtime/javaCalls.hpp" |
kamg@2511 | 28 | #include "runtime/serviceThread.hpp" |
kamg@2511 | 29 | #include "runtime/mutexLocker.hpp" |
kamg@2511 | 30 | #include "prims/jvmtiImpl.hpp" |
fparain@2888 | 31 | #include "services/gcNotifier.hpp" |
kamg@2511 | 32 | |
kamg@2511 | 33 | ServiceThread* ServiceThread::_instance = NULL; |
kamg@2511 | 34 | |
kamg@2511 | 35 | void ServiceThread::initialize() { |
kamg@2511 | 36 | EXCEPTION_MARK; |
kamg@2511 | 37 | |
kamg@2511 | 38 | instanceKlassHandle klass (THREAD, SystemDictionary::Thread_klass()); |
kamg@2511 | 39 | instanceHandle thread_oop = klass->allocate_instance_handle(CHECK); |
kamg@2511 | 40 | |
kamg@2511 | 41 | const char* name = JDK_Version::is_gte_jdk17x_version() ? |
kamg@2511 | 42 | "Service Thread" : "Low Memory Detector"; |
kamg@2511 | 43 | |
kamg@2511 | 44 | Handle string = java_lang_String::create_from_str(name, CHECK); |
kamg@2511 | 45 | |
kamg@2511 | 46 | // Initialize thread_oop to put it into the system threadGroup |
kamg@2511 | 47 | Handle thread_group (THREAD, Universe::system_thread_group()); |
kamg@2511 | 48 | JavaValue result(T_VOID); |
kamg@2511 | 49 | JavaCalls::call_special(&result, thread_oop, |
kamg@2511 | 50 | klass, |
kamg@2511 | 51 | vmSymbols::object_initializer_name(), |
kamg@2511 | 52 | vmSymbols::threadgroup_string_void_signature(), |
kamg@2511 | 53 | thread_group, |
kamg@2511 | 54 | string, |
kamg@2511 | 55 | CHECK); |
kamg@2511 | 56 | |
kamg@2511 | 57 | { |
kamg@2511 | 58 | MutexLocker mu(Threads_lock); |
kamg@2511 | 59 | ServiceThread* thread = new ServiceThread(&service_thread_entry); |
kamg@2511 | 60 | |
kamg@2511 | 61 | // At this point it may be possible that no osthread was created for the |
kamg@2511 | 62 | // JavaThread due to lack of memory. We would have to throw an exception |
kamg@2511 | 63 | // in that case. However, since this must work and we do not allow |
kamg@2511 | 64 | // exceptions anyway, check and abort if this fails. |
kamg@2511 | 65 | if (thread == NULL || thread->osthread() == NULL) { |
kamg@2511 | 66 | vm_exit_during_initialization("java.lang.OutOfMemoryError", |
kamg@2511 | 67 | "unable to create new native thread"); |
kamg@2511 | 68 | } |
kamg@2511 | 69 | |
kamg@2511 | 70 | java_lang_Thread::set_thread(thread_oop(), thread); |
kamg@2511 | 71 | java_lang_Thread::set_priority(thread_oop(), NearMaxPriority); |
kamg@2511 | 72 | java_lang_Thread::set_daemon(thread_oop()); |
kamg@2511 | 73 | thread->set_threadObj(thread_oop()); |
dcubed@2623 | 74 | _instance = thread; |
kamg@2511 | 75 | |
kamg@2511 | 76 | Threads::add(thread); |
kamg@2511 | 77 | Thread::start(thread); |
kamg@2511 | 78 | } |
kamg@2511 | 79 | } |
kamg@2511 | 80 | |
kamg@2511 | 81 | void ServiceThread::service_thread_entry(JavaThread* jt, TRAPS) { |
kamg@2511 | 82 | while (true) { |
kamg@2511 | 83 | bool sensors_changed = false; |
kamg@2511 | 84 | bool has_jvmti_events = false; |
fparain@2888 | 85 | bool has_gc_notification_event = false; |
kamg@2511 | 86 | JvmtiDeferredEvent jvmti_event; |
kamg@2511 | 87 | { |
kamg@2511 | 88 | // Need state transition ThreadBlockInVM so that this thread |
kamg@2511 | 89 | // will be handled by safepoint correctly when this thread is |
kamg@2511 | 90 | // notified at a safepoint. |
kamg@2511 | 91 | |
kamg@2511 | 92 | // This ThreadBlockInVM object is not also considered to be |
kamg@2511 | 93 | // suspend-equivalent because ServiceThread is not visible to |
kamg@2511 | 94 | // external suspension. |
kamg@2511 | 95 | |
kamg@2511 | 96 | ThreadBlockInVM tbivm(jt); |
kamg@2511 | 97 | |
kamg@2511 | 98 | MutexLockerEx ml(Service_lock, Mutex::_no_safepoint_check_flag); |
kamg@2511 | 99 | while (!(sensors_changed = LowMemoryDetector::has_pending_requests()) && |
fparain@2888 | 100 | !(has_jvmti_events = JvmtiDeferredEventQueue::has_events()) && |
fparain@2888 | 101 | !(has_gc_notification_event = GCNotifier::has_event())) { |
kamg@2511 | 102 | // wait until one of the sensors has pending requests, or there is a |
fparain@2888 | 103 | // pending JVMTI event or JMX GC notification to post |
kamg@2511 | 104 | Service_lock->wait(Mutex::_no_safepoint_check_flag); |
kamg@2511 | 105 | } |
kamg@2511 | 106 | |
kamg@2511 | 107 | if (has_jvmti_events) { |
kamg@2511 | 108 | jvmti_event = JvmtiDeferredEventQueue::dequeue(); |
kamg@2511 | 109 | } |
kamg@2511 | 110 | } |
kamg@2511 | 111 | |
kamg@2511 | 112 | if (has_jvmti_events) { |
kamg@2511 | 113 | jvmti_event.post(); |
kamg@2511 | 114 | } |
kamg@2511 | 115 | |
kamg@2511 | 116 | if (sensors_changed) { |
kamg@2511 | 117 | LowMemoryDetector::process_sensor_changes(jt); |
kamg@2511 | 118 | } |
fparain@2888 | 119 | |
fparain@2888 | 120 | if(has_gc_notification_event) { |
fparain@2888 | 121 | GCNotifier::sendNotification(CHECK); |
fparain@2888 | 122 | } |
kamg@2511 | 123 | } |
kamg@2511 | 124 | } |
kamg@2511 | 125 | |
kamg@2511 | 126 | bool ServiceThread::is_service_thread(Thread* thread) { |
kamg@2511 | 127 | return thread == _instance; |
kamg@2511 | 128 | } |