jfrThreadLocal.cpp 5.5 KB
Newer Older
A
apetushkov 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 *
 */

#include "precompiled.hpp"
26
#include "jfr/jfrEvents.hpp"
A
apetushkov 已提交
27 28 29 30 31 32 33 34 35 36 37 38
#include "jfr/jni/jfrJavaSupport.hpp"
#include "jfr/periodic/jfrThreadCPULoadEvent.hpp"
#include "jfr/recorder/jfrRecorder.hpp"
#include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
#include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp"
#include "jfr/recorder/service/jfrOptionSet.hpp"
#include "jfr/recorder/storage/jfrStorage.hpp"
#include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp"
#include "jfr/support/jfrThreadLocal.hpp"
#include "memory/allocation.inline.hpp"
#include "runtime/os.hpp"
#include "runtime/thread.inline.hpp"
39
#include "utilities/sizes.hpp"
A
apetushkov 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

/* This data structure is per thread and only accessed by the thread itself, no locking required */
JfrThreadLocal::JfrThreadLocal() :
  _java_event_writer(NULL),
  _java_buffer(NULL),
  _native_buffer(NULL),
  _shelved_buffer(NULL),
  _stackframes(NULL),
  _trace_id(JfrTraceId::assign_thread_id()),
  _thread_cp(),
  _data_lost(0),
  _stack_trace_id(max_julong),
  _user_time(0),
  _cpu_time(0),
  _wallclock_time(os::javaTimeNanos()),
  _stack_trace_hash(0),
  _stackdepth(0),
  _entering_suspend_flag(0),
  _dead(false) {}

u8 JfrThreadLocal::add_data_lost(u8 value) {
  _data_lost += value;
  return _data_lost;
}

bool JfrThreadLocal::has_thread_checkpoint() const {
  return _thread_cp.valid();
}

void JfrThreadLocal::set_thread_checkpoint(const JfrCheckpointBlobHandle& ref) {
  assert(!_thread_cp.valid(), "invariant");
  _thread_cp = ref;
}

const JfrCheckpointBlobHandle& JfrThreadLocal::thread_checkpoint() const {
  return _thread_cp;
}

78 79 80 81
static void send_java_thread_start_event(JavaThread* jt) {
  EventThreadStart event;
  event.set_thread(jt->jfr_thread_local()->thread_id());
  event.commit();
A
apetushkov 已提交
82 83
}

84 85 86
void JfrThreadLocal::on_start(Thread* t) {
  assert(t != NULL, "invariant");
  assert(Thread::current() == t, "invariant");
A
apetushkov 已提交
87
  if (JfrRecorder::is_recording()) {
88 89 90
    if (t->is_Java_thread()) {
      send_java_thread_start_event((JavaThread*)t);
    }
A
apetushkov 已提交
91 92 93
  }
}

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
static void send_java_thread_end_events(traceid id, JavaThread* jt) {
  assert(jt != NULL, "invariant");
  assert(Thread::current() == jt, "invariant");
  assert(jt->jfr_thread_local()->trace_id() == id, "invariant");
  EventThreadEnd event;
  event.set_thread(id);
  event.commit();
  JfrThreadCPULoadEvent::send_event_for_thread(jt);
}

void JfrThreadLocal::release(JfrThreadLocal* tl, Thread* t) {
  assert(tl != NULL, "invariant");
  assert(t != NULL, "invariant");
  assert(Thread::current() == t, "invariant");
  assert(!tl->is_dead(), "invariant");
  assert(tl->shelved_buffer() == NULL, "invariant");
A
apetushkov 已提交
110
  if (tl->has_native_buffer()) {
111
    JfrStorage::release_thread_local(tl->native_buffer(), t);
A
apetushkov 已提交
112 113
  }
  if (tl->has_java_buffer()) {
114
    JfrStorage::release_thread_local(tl->java_buffer(), t);
A
apetushkov 已提交
115
  }
116 117
  if (tl->has_java_event_writer()) {
    assert(t->is_Java_thread(), "invariant");
A
apetushkov 已提交
118 119
    JfrJavaSupport::destroy_global_jni_handle(tl->java_event_writer());
  }
120 121 122 123
  if (tl->_stackframes != NULL) {
    FREE_C_HEAP_ARRAY(JfrStackFrame, tl->_stackframes, mtTracing);
  }
  tl->_dead = true;
A
apetushkov 已提交
124 125
}

126 127 128 129 130 131 132 133 134 135
void JfrThreadLocal::on_exit(Thread* t) {
  assert(t != NULL, "invariant");
  JfrThreadLocal * const tl = t->jfr_thread_local();
  assert(!tl->is_dead(), "invariant");
  if (JfrRecorder::is_recording()) {
    if (t->is_Java_thread()) {
      send_java_thread_end_events(tl->thread_id(), (JavaThread*)t);
    }
  }
  release(tl, Thread::current()); // because it could be that Thread::current() != t
A
apetushkov 已提交
136 137 138 139
}

JfrBuffer* JfrThreadLocal::install_native_buffer() const {
  assert(!has_native_buffer(), "invariant");
140
  _native_buffer = JfrStorage::acquire_thread_local(Thread::current());
A
apetushkov 已提交
141 142 143 144 145 146
  return _native_buffer;
}

JfrBuffer* JfrThreadLocal::install_java_buffer() const {
  assert(!has_java_buffer(), "invariant");
  assert(!has_java_event_writer(), "invariant");
147
  _java_buffer = JfrStorage::acquire_thread_local(Thread::current());
A
apetushkov 已提交
148 149 150 151 152
  return _java_buffer;
}

JfrStackFrame* JfrThreadLocal::install_stackframes() const {
  assert(_stackframes == NULL, "invariant");
153
  _stackframes = NEW_C_HEAP_ARRAY(JfrStackFrame, stackdepth(), mtTracing);
A
apetushkov 已提交
154 155 156
  return _stackframes;
}

157 158 159 160 161 162
ByteSize JfrThreadLocal::trace_id_offset() {
  return in_ByteSize(offset_of(JfrThreadLocal, _trace_id));
}

ByteSize JfrThreadLocal::java_event_writer_offset() {
  return in_ByteSize(offset_of(JfrThreadLocal, _java_event_writer));
A
apetushkov 已提交
163
}
164 165 166 167

u4 JfrThreadLocal::stackdepth() const {
  return _stackdepth != 0 ? _stackdepth : (u4)JfrOptionSet::stackdepth();
}