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
34JfrFlush::JfrFlush(JfrStorage::Buffer* old, size_t used, size_t requested, Thread* t) :
35 _result(JfrStorage::flush(old, used, requested, true, t)) {
36}
37
38template <typename T>
39class LessThanHalfBufferSize : AllStatic {
40public:
41 static bool evaluate(T* t) {
42 assert(t != NULL, "invariant");
43 return t->free_size() < t->size() / 2;
44 }
45};
46
47template <typename T>
48class 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
56bool jfr_is_event_enabled(JfrEventId id) {
57 return JfrEventSetting::is_enabled(id);
58}
59
60bool jfr_has_stacktrace_enabled(JfrEventId id) {
61 return JfrEventSetting::has_stacktrace(id);
62}
63
64void jfr_conditional_flush(JfrEventId id, size_t size, Thread* t) {
65 if (t->jfr_thread_local()->has_native_buffer()) {
66 JfrStorage::Buffer* const buffer = t->jfr_thread_local()->native_buffer();
67 if (LessThanSize<JfrStorage::Buffer>::evaluate(buffer, size)) {
68 JfrFlush f(buffer, 0, 0, t);
69 }
70 }
71}
72
73bool jfr_save_stacktrace(Thread* t) {
74 JfrThreadLocal* const tl = t->jfr_thread_local();
75 if (tl->has_cached_stack_trace()) {
76 return false; // no ownership
77 }
78 tl->set_cached_stack_trace_id(JfrStackTraceRepository::record(t));
79 return true;
80}
81
82void jfr_clear_stacktrace(Thread* t) {
83 t->jfr_thread_local()->clear_cached_stack_trace();
84}
85