1 /*
   2  * Copyright (c) 2016, 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/jfrEventSetting.inline.hpp"
  27 #include "jfr/recorder/storage/jfrStorage.hpp"
  28 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
  29 #include "jfr/support/jfrFlush.hpp"
  30 #include "jfr/support/jfrThreadLocal.hpp"
  31 #include "runtime/thread.inline.hpp"
  32 #include "utilities/debug.hpp"
  33 
  34 JfrFlush::JfrFlush(JfrStorage::Buffer* old, size_t used, size_t requested, Thread* t) :
  35   _result(JfrStorage::flush(old, used, requested, true, t)) {
  36 }
  37 
  38 template <typename T>
  39 class LessThanHalfBufferSize : AllStatic {
  40 public:
  41   static bool evaluate(T* t) {
  42     assert(t != NULL, "invariant");
  43     return t->free_size() < t->size() / 2;
  44   }
  45 };
  46 
  47 template <typename T>
  48 class LessThanSize : AllStatic {
  49  public:
  50   static bool evaluate(T* t, size_t size) {
  51     assert(t != NULL, "invariant");
  52     return t->free_size() < size;
  53   }
  54 };
  55 
  56 bool jfr_is_event_enabled(JfrEventId id) {
  57   return JfrEventSetting::is_enabled(id);
  58 }
  59 
  60 bool jfr_has_stacktrace_enabled(JfrEventId id) {
  61   return JfrEventSetting::has_stacktrace(id);
  62 }
  63 
  64 void jfr_conditional_flush(JfrEventId id, size_t size, Thread* t) {
  65   assert(jfr_is_event_enabled(id), "invariant");
  66   if (t->jfr_thread_local()->has_native_buffer()) {
  67     JfrStorage::Buffer* const buffer = t->jfr_thread_local()->native_buffer();
  68     if (LessThanSize<JfrStorage::Buffer>::evaluate(buffer, size)) {
  69       JfrFlush f(buffer, 0, 0, t);
  70     }
  71   }
  72 }
  73 
  74 bool jfr_save_stacktrace(Thread* t) {
  75   JfrThreadLocal* const tl = t->jfr_thread_local();
  76   if (tl->has_cached_stack_trace()) {
  77     return false; // no ownership
  78   }
  79   tl->set_cached_stack_trace_id(JfrStackTraceRepository::record(t));
  80   return true;
  81 }
  82 
  83 void jfr_clear_stacktrace(Thread* t) {
  84   t->jfr_thread_local()->clear_cached_stack_trace();
  85 }