1 /*
   2  * Copyright (c) 2012, 2018, 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/jfrRecorder.hpp"
  27 #include "jfr/recorder/service/jfrPostBox.hpp"
  28 #include "jfr/recorder/service/jfrRecorderService.hpp"
  29 #include "jfr/recorder/service/jfrRecorderThread.hpp"
  30 #include "runtime/mutexLocker.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 
  33 //
  34 // Entry point for "JFR Recorder Thread" message loop.
  35 // The recorder thread executes service requests collected from the message system.
  36 //
  37 void recorderthread_entry(JavaThread* thread, Thread* unused) {
  38   assert(thread != NULL, "invariant");
  39   #define START (msgs & (MSGBIT(MSG_START)))
  40   #define SHUTDOWN (msgs & MSGBIT(MSG_SHUTDOWN))
  41   #define ROTATE (msgs & (MSGBIT(MSG_ROTATE)|MSGBIT(MSG_STOP)))
  42   #define PROCESS_FULL_BUFFERS (msgs & (MSGBIT(MSG_ROTATE)|MSGBIT(MSG_STOP)|MSGBIT(MSG_FULLBUFFER)))
  43   #define SCAVENGE (msgs & (MSGBIT(MSG_DEADBUFFER)))
  44 
  45   JfrPostBox& post_box = JfrRecorderThread::post_box();
  46   if (LogJFR) tty->print_cr("Recorder thread STARTED");
  47 
  48   {
  49     bool done = false;
  50     int msgs = 0;
  51     JfrRecorderService service;
  52     MutexLockerEx msg_lock(JfrMsg_lock);
  53 
  54     // JFR MESSAGE LOOP PROCESSING - BEGIN
  55     while (!done) {
  56       if (post_box.is_empty()) {
  57         JfrMsg_lock->wait(false);
  58       }
  59       msgs = post_box.collect();
  60       JfrMsg_lock->unlock();
  61       if (PROCESS_FULL_BUFFERS) {
  62         service.process_full_buffers();
  63       }
  64       if (SCAVENGE) {
  65         service.scavenge();
  66       }
  67       // Check amount of data written to chunk already
  68       // if it warrants asking for a new chunk
  69       service.evaluate_chunk_size_for_rotation();
  70       if (START) {
  71         service.start();
  72       } else if (ROTATE) {
  73         service.rotate(msgs);
  74       }
  75       JfrMsg_lock->lock();
  76       post_box.notify_waiters();
  77       if (SHUTDOWN) {
  78         if (LogJFR) tty->print_cr("Request to STOP recorder");
  79         done = true;
  80       }
  81     } // JFR MESSAGE LOOP PROCESSING - END
  82 
  83   } // JfrMsg_lock scope
  84 
  85   assert(!JfrMsg_lock->owned_by_self(), "invariant");
  86   post_box.notify_collection_stop();
  87   JfrRecorder::on_recorder_thread_exit();
  88 
  89   #undef START
  90   #undef SHUTDOWN
  91   #undef ROTATE
  92   #undef PROCESS_FULL_BUFFERS
  93   #undef SCAVENGE
  94 }