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/jni/jfrJavaSupport.hpp"
  27 #include "jfr/recorder/repository/jfrChunkRotation.hpp"
  28 #include "jfr/recorder/repository/jfrChunkWriter.hpp"
  29 
  30 static jobject chunk_monitor = NULL;
  31 static intptr_t threshold = 0;
  32 static bool rotate = false;
  33 
  34 static jobject install_chunk_monitor(Thread* thread) {
  35   assert(chunk_monitor == NULL, "invariant");
  36   // read static field
  37   HandleMark hm(thread);
  38   static const char klass[] = "jdk/jfr/internal/JVM";
  39   static const char field[] = "FILE_DELTA_CHANGE";
  40   static const char signature[] = "Ljava/lang/Object;";
  41   JavaValue result(T_OBJECT);
  42   JfrJavaArguments field_args(&result, klass, field, signature, thread);
  43   JfrJavaSupport::get_field_global_ref(&field_args, thread);
  44   chunk_monitor = result.get_jobject();
  45   return chunk_monitor;
  46 }
  47 
  48 // lazy install
  49 static jobject get_chunk_monitor(Thread* thread) {
  50   return chunk_monitor != NULL ? chunk_monitor : install_chunk_monitor(thread);
  51 }
  52 
  53 static void notify() {
  54   Thread* const thread = Thread::current();
  55   JfrJavaSupport::notify_all(get_chunk_monitor(thread), thread);
  56 }
  57 
  58 void JfrChunkRotation::evaluate(const JfrChunkWriter& writer) {
  59   assert(threshold > 0, "invariant");
  60   if (rotate) {
  61     // already in progress
  62     return;
  63   }
  64   assert(!rotate, "invariant");
  65   if (writer.size_written() > threshold) {
  66     rotate = true;
  67     notify();
  68   }
  69 }
  70 
  71 bool JfrChunkRotation::should_rotate() {
  72   return rotate;
  73 }
  74 
  75 void JfrChunkRotation::on_rotation() {
  76   rotate = false;
  77 }
  78 
  79 void JfrChunkRotation::set_threshold(intptr_t bytes) {
  80   threshold = bytes;
  81 }