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