1 /*
   2  * Copyright (c) 2013, 2019, 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 "jfr/recorder/service/jfrPostBox.hpp"
  27 #include "jfr/utilities/jfrTryLock.inline.hpp"
  28 #include "runtime/atomic.hpp"
  29 #include "runtime/mutexLocker.inline.hpp"
  30 #include "runtime/orderAccess.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 
  33 #define MSG_IS_SYNCHRONOUS ( (MSGBIT(MSG_ROTATE)) |          \
  34                              (MSGBIT(MSG_STOP))   |          \
  35                              (MSGBIT(MSG_START))  |          \
  36                              (MSGBIT(MSG_CLONE_IN_MEMORY)) | \
  37                              (MSGBIT(MSG_VM_ERROR))          \
  38                            )
  39 
  40 static JfrPostBox* _instance = NULL;
  41 
  42 JfrPostBox& JfrPostBox::instance() {
  43   return *_instance;
  44 }
  45 
  46 JfrPostBox* JfrPostBox::create() {
  47   assert(_instance == NULL, "invariant");
  48   _instance = new JfrPostBox();
  49   return _instance;
  50 }
  51 
  52 void JfrPostBox::destroy() {
  53   assert(_instance != NULL, "invariant");
  54   delete _instance;
  55   _instance = NULL;
  56 }
  57 
  58 JfrPostBox::JfrPostBox() :
  59   _msg_read_serial(0),
  60   _msg_handled_serial(0),
  61   _messages(0),
  62   _has_waiters(false) {}
  63 
  64 static bool is_thread_lock_aversive() {
  65   Thread* const thread = Thread::current();
  66   return (thread->is_Java_thread() && ((JavaThread*)thread)->thread_state() != _thread_in_vm) || thread->is_VM_thread();
  67 }
  68 
  69 static bool is_synchronous(int messages) {
  70   return ((messages & MSG_IS_SYNCHRONOUS) != 0);
  71 }
  72 
  73 void JfrPostBox::post(JFR_Msg msg) {
  74   const int the_message = MSGBIT(msg);
  75   if (is_thread_lock_aversive()) {
  76     deposit(the_message);
  77     return;
  78   }
  79   if (!is_synchronous(the_message)) {
  80     asynchronous_post(the_message);
  81     return;
  82   }
  83   synchronous_post(the_message);
  84 }
  85 
  86 void JfrPostBox::deposit(int new_messages) {
  87   while (true) {
  88     const int current_msgs = OrderAccess::load_acquire(&_messages);
  89     // OR the new message
  90     const int exchange_value = current_msgs | new_messages;
  91     const int result = Atomic::cmpxchg(exchange_value, &_messages, current_msgs);
  92     if (result == current_msgs) {
  93       return;
  94     }
  95     /* Some other thread just set exactly what this thread wanted */
  96     if ((result & new_messages) == new_messages) {
  97       return;
  98     }
  99   }
 100 }
 101 
 102 void JfrPostBox::asynchronous_post(int msg) {
 103   assert(!is_synchronous(msg), "invariant");
 104   deposit(msg);
 105   JfrMonitorTryLock try_msg_lock(JfrMsg_lock);
 106   if (try_msg_lock.acquired()) {
 107     JfrMsg_lock->notify_all();
 108   }
 109 }
 110 
 111 void JfrPostBox::synchronous_post(int msg) {
 112   assert(is_synchronous(msg), "invariant");
 113   assert(!JfrMsg_lock->owned_by_self(), "should not hold JfrMsg_lock here!");
 114   MonitorLocker msg_lock(JfrMsg_lock);
 115   deposit(msg);
 116   // serial_id is used to check when what we send in has been processed.
 117   // _msg_read_serial is read under JfrMsg_lock protection.
 118   const uintptr_t serial_id = OrderAccess::load_acquire(&_msg_read_serial) + 1;
 119   msg_lock.notify_all();
 120   while (!is_message_processed(serial_id)) {
 121     msg_lock.wait();
 122   }
 123 }
 124 
 125 /*
 126  * Check if a synchronous message has been processed.
 127  * We avoid racing on _msg_handled_serial by ensuring
 128  * that we are holding the JfrMsg_lock when checking
 129  * completion status.
 130  */
 131 bool JfrPostBox::is_message_processed(uintptr_t serial_id) const {
 132   assert(JfrMsg_lock->owned_by_self(), "_msg_handled_serial must be read under JfrMsg_lock protection");
 133   return serial_id <= OrderAccess::load_acquire(&_msg_handled_serial);
 134 }
 135 
 136 bool JfrPostBox::is_empty() const {
 137   assert(JfrMsg_lock->owned_by_self(), "not holding JfrMsg_lock!");
 138   return OrderAccess::load_acquire(&_messages) == 0;
 139 }
 140 
 141 int JfrPostBox::collect() {
 142   // get pending and reset to 0
 143   const int messages = Atomic::xchg(0, &_messages);
 144   if (check_waiters(messages)) {
 145     _has_waiters = true;
 146     assert(JfrMsg_lock->owned_by_self(), "incrementing _msg_read_serial is protected by JfrMsg_lock");
 147     // Update made visible on release of JfrMsg_lock via fence instruction in Monitor::IUnlock.
 148     ++_msg_read_serial;
 149   }
 150   return messages;
 151 }
 152 
 153 bool JfrPostBox::check_waiters(int messages) const {
 154   assert(JfrMsg_lock->owned_by_self(), "not holding JfrMsg_lock!");
 155   assert(!_has_waiters, "invariant");
 156   return is_synchronous(messages);
 157 }
 158 
 159 void JfrPostBox::notify_waiters() {
 160   if (!_has_waiters) {
 161     return;
 162   }
 163   _has_waiters = false;
 164   assert(JfrMsg_lock->owned_by_self(), "incrementing _msg_handled_serial is protected by JfrMsg_lock.");
 165   // Update made visible on release of JfrMsg_lock via fence instruction in Monitor::IUnlock.
 166   ++_msg_handled_serial;
 167   JfrMsg_lock->notify();
 168 }
 169 
 170 // safeguard to ensure no threads are left waiting
 171 void JfrPostBox::notify_collection_stop() {
 172   MutexLocker msg_lock(JfrMsg_lock);
 173   JfrMsg_lock->notify_all();
 174 }