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/jfrChunkWriter.hpp"
  28 #include "jfr/recorder/repository/jfrChunkSizeNotifier.hpp"
  29 
  30 size_t JfrChunkSizeNotifier::_chunk_size_threshold = 0;
  31 
  32 void JfrChunkSizeNotifier::set_chunk_size_threshold(size_t bytes) {
  33   _chunk_size_threshold = bytes;
  34 }
  35 
  36 size_t JfrChunkSizeNotifier::chunk_size_threshold() {
  37   return _chunk_size_threshold;
  38 }
  39 
  40 static jobject new_chunk_monitor = NULL;
  41 
  42 // lazy install
  43 static jobject get_new_chunk_monitor(Thread* thread) {
  44   static bool initialized = false;
  45   if (initialized) {
  46     assert(new_chunk_monitor != NULL, "invariant");
  47     return new_chunk_monitor;
  48   }
  49   assert(new_chunk_monitor == NULL, "invariant");
  50   // read static field
  51   HandleMark hm(thread);
  52   static const char klass[] = "jdk/jfr/internal/JVM";
  53   static const char field[] = "FILE_DELTA_CHANGE";
  54   static const char signature[] = "Ljava/lang/Object;";
  55   JavaValue result(T_OBJECT);
  56   JfrJavaArguments field_args(&result, klass, field, signature, thread);
  57   JfrJavaSupport::get_field_global_ref(&field_args, thread);
  58   new_chunk_monitor = result.get_jobject();
  59   initialized = new_chunk_monitor != NULL;
  60   return new_chunk_monitor;
  61 }
  62 
  63 void JfrChunkSizeNotifier::notify() {
  64   Thread* const thread = Thread::current();
  65   JfrJavaSupport::notify_all(get_new_chunk_monitor(thread), thread);
  66 }
  67 
  68 void JfrChunkSizeNotifier::release_monitor() {
  69   if (new_chunk_monitor != NULL) {
  70     JfrJavaSupport::destroy_global_jni_handle(new_chunk_monitor);
  71     new_chunk_monitor = NULL;
  72   }
  73 }