src/hotspot/share/runtime/notificationThread.cpp
author mgronlun
Wed, 30 Oct 2019 19:43:52 +0100
changeset 58863 c16ac7a2eba4
parent 58503 726a3945e934
permissions -rw-r--r--
8226511: Implement JFR Event Streaming Reviewed-by: egahlin, mseledtsov, mgronlun Contributed-by: erik.gahlin@oracle.com, mikhailo.seledtsov@oracle.com, markus.gronlund@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
58503
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     1
/*
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     4
 *
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     7
 * published by the Free Software Foundation.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     8
 *
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    13
 * accompanied this code).
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    14
 *
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    18
 *
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    21
 * questions.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    22
 *
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    23
 */
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    24
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    25
#include "precompiled.hpp"
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    26
#include "memory/universe.hpp"
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    27
#include "runtime/interfaceSupport.inline.hpp"
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    28
#include "runtime/javaCalls.hpp"
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    29
#include "runtime/notificationThread.hpp"
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    30
#include "services/diagnosticArgument.hpp"
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    31
#include "services/diagnosticFramework.hpp"
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    32
#include "services/gcNotifier.hpp"
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    33
#include "services/lowMemoryDetector.hpp"
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    34
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    35
NotificationThread* NotificationThread::_instance = NULL;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    36
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    37
void NotificationThread::initialize() {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    38
  EXCEPTION_MARK;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    39
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    40
  const char* name = "Notification Thread";
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    41
  Handle string = java_lang_String::create_from_str(name, CHECK);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    42
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    43
  // Initialize thread_oop to put it into the system threadGroup
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    44
  Handle thread_group (THREAD, Universe::system_thread_group());
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    45
  Handle thread_oop = JavaCalls::construct_new_instance(
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    46
                          SystemDictionary::Thread_klass(),
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    47
                          vmSymbols::threadgroup_string_void_signature(),
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    48
                          thread_group,
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    49
                          string,
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    50
                          CHECK);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    51
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    52
  Klass* group = SystemDictionary::ThreadGroup_klass();
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    53
  JavaValue result(T_VOID);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    54
  JavaCalls::call_special(&result,
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    55
                          thread_group,
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    56
                          group,
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    57
                          vmSymbols::add_method_name(),
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    58
                          vmSymbols::thread_void_signature(),
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    59
                          thread_oop,
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    60
                          THREAD);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    61
  {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    62
    MutexLocker mu(Threads_lock);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    63
    NotificationThread* thread =  new NotificationThread(&notification_thread_entry);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    64
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    65
    // At this point it may be possible that no osthread was created for the
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    66
    // JavaThread due to lack of memory. We would have to throw an exception
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    67
    // in that case. However, since this must work and we do not allow
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    68
    // exceptions anyway, check and abort if this fails.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    69
    if (thread == NULL || thread->osthread() == NULL) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    70
      vm_exit_during_initialization("java.lang.OutOfMemoryError",
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    71
                                    os::native_thread_creation_failed_msg());
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    72
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    73
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    74
    java_lang_Thread::set_thread(thread_oop(), thread);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    75
    java_lang_Thread::set_priority(thread_oop(), NearMaxPriority);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    76
    java_lang_Thread::set_daemon(thread_oop());
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    77
    thread->set_threadObj(thread_oop());
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    78
    _instance = thread;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    79
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    80
    Threads::add(thread);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    81
    Thread::start(thread);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    82
  }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    83
}
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    84
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    85
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    86
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    87
void NotificationThread::notification_thread_entry(JavaThread* jt, TRAPS) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    88
  while (true) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    89
    bool sensors_changed = false;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    90
    bool has_dcmd_notification_event = false;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    91
    bool has_gc_notification_event = false;
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    92
    {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    93
      // Need state transition ThreadBlockInVM so that this thread
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    94
      // will be handled by safepoint correctly when this thread is
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    95
      // notified at a safepoint.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    96
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    97
      ThreadBlockInVM tbivm(jt);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    98
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
    99
      MonitorLocker ml(Notification_lock, Mutex::_no_safepoint_check_flag);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   100
      // Process all available work on each (outer) iteration, rather than
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   101
      // only the first recognized bit of work, to avoid frequently true early
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   102
      // tests from potentially starving later work.  Hence the use of
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   103
      // arithmetic-or to combine results; we don't want short-circuiting.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   104
      while (((sensors_changed = LowMemoryDetector::has_pending_requests()) |
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   105
              (has_dcmd_notification_event = DCmdFactory::has_pending_jmx_notification()) |
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   106
              (has_gc_notification_event = GCNotifier::has_event()))
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   107
             == 0) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   108
        // Wait as a suspend equalent until notified that there is some work to do.
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   109
        ml.wait(0, true);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   110
      }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   111
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   112
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   113
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   114
    if (sensors_changed) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   115
      LowMemoryDetector::process_sensor_changes(jt);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   116
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   117
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   118
    if(has_gc_notification_event) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   119
      GCNotifier::sendNotification(CHECK);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   120
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   121
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   122
    if(has_dcmd_notification_event) {
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   123
      DCmdFactory::send_notification(CHECK);
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   124
    }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   125
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   126
  }
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   127
}
726a3945e934 8170299: Debugger does not stop inside the low memory notifications code
dtitov
parents:
diff changeset
   128