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