src/hotspot/share/jfr/support/jfrThreadLocal.cpp
branchJEP-349-branch
changeset 57360 5d043a159d5c
parent 52569 1a534c7926cc
child 57870 00860d9caf4d
equal deleted inserted replaced
57359:4cab5edc2950 57360:5d043a159d5c
     1 /*
     1 /*
     2  * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    53   _cpu_time(0),
    53   _cpu_time(0),
    54   _wallclock_time(os::javaTimeNanos()),
    54   _wallclock_time(os::javaTimeNanos()),
    55   _stack_trace_hash(0),
    55   _stack_trace_hash(0),
    56   _stackdepth(0),
    56   _stackdepth(0),
    57   _entering_suspend_flag(0),
    57   _entering_suspend_flag(0),
       
    58   _excluded(false),
    58   _dead(false) {}
    59   _dead(false) {}
    59 
    60 
    60 u8 JfrThreadLocal::add_data_lost(u8 value) {
    61 u8 JfrThreadLocal::add_data_lost(u8 value) {
    61   _data_lost += value;
    62   _data_lost += value;
    62   return _data_lost;
    63   return _data_lost;
    82 }
    83 }
    83 
    84 
    84 void JfrThreadLocal::on_start(Thread* t) {
    85 void JfrThreadLocal::on_start(Thread* t) {
    85   assert(t != NULL, "invariant");
    86   assert(t != NULL, "invariant");
    86   assert(Thread::current() == t, "invariant");
    87   assert(Thread::current() == t, "invariant");
       
    88   JfrJavaSupport::on_thread_start(t);
    87   if (JfrRecorder::is_recording()) {
    89   if (JfrRecorder::is_recording()) {
    88     if (t->is_Java_thread()) {
    90     if (!t->jfr_thread_local()->is_excluded()) {
    89       send_java_thread_start_event((JavaThread*)t);
    91       JfrCheckpointManager::write_thread_checkpoint(t);
       
    92       if (t->is_Java_thread()) {
       
    93         send_java_thread_start_event((JavaThread*)t);
       
    94       }
    90     }
    95     }
    91   }
    96   }
    92 }
    97 }
    93 
    98 
    94 static void send_java_thread_end_events(traceid id, JavaThread* jt) {
    99 static void send_java_thread_end_events(traceid id, JavaThread* jt) {
   105   assert(tl != NULL, "invariant");
   110   assert(tl != NULL, "invariant");
   106   assert(t != NULL, "invariant");
   111   assert(t != NULL, "invariant");
   107   assert(Thread::current() == t, "invariant");
   112   assert(Thread::current() == t, "invariant");
   108   assert(!tl->is_dead(), "invariant");
   113   assert(!tl->is_dead(), "invariant");
   109   assert(tl->shelved_buffer() == NULL, "invariant");
   114   assert(tl->shelved_buffer() == NULL, "invariant");
       
   115   tl->_dead = true;
       
   116   if (tl->has_java_event_writer()) {
       
   117     assert(t->is_Java_thread(), "invariant");
       
   118     const jobject event_writer = tl->java_event_writer();
       
   119     tl->set_java_event_writer(NULL);
       
   120     JfrJavaSupport::destroy_global_jni_handle(event_writer);
       
   121   }
   110   if (tl->has_native_buffer()) {
   122   if (tl->has_native_buffer()) {
   111     JfrStorage::release_thread_local(tl->native_buffer(), t);
   123     JfrStorage::release_thread_local(tl->native_buffer(), t);
   112   }
   124   }
   113   if (tl->has_java_buffer()) {
   125   if (tl->has_java_buffer()) {
   114     JfrStorage::release_thread_local(tl->java_buffer(), t);
   126     JfrStorage::release_thread_local(tl->java_buffer(), t);
   115   }
   127   }
   116   if (tl->has_java_event_writer()) {
       
   117     assert(t->is_Java_thread(), "invariant");
       
   118     JfrJavaSupport::destroy_global_jni_handle(tl->java_event_writer());
       
   119   }
       
   120   if (tl->_stackframes != NULL) {
   128   if (tl->_stackframes != NULL) {
   121     FREE_C_HEAP_ARRAY(JfrStackFrame, tl->_stackframes);
   129     FREE_C_HEAP_ARRAY(JfrStackFrame, tl->_stackframes);
   122   }
   130   }
   123   tl->_dead = true;
       
   124 }
   131 }
   125 
   132 
   126 void JfrThreadLocal::on_exit(Thread* t) {
   133 void JfrThreadLocal::on_exit(Thread* t) {
   127   assert(t != NULL, "invariant");
   134   assert(t != NULL, "invariant");
   128   JfrThreadLocal * const tl = t->jfr_thread_local();
   135   JfrThreadLocal * const tl = t->jfr_thread_local();
   129   assert(!tl->is_dead(), "invariant");
   136   assert(!tl->is_dead(), "invariant");
   130   if (JfrRecorder::is_recording()) {
   137   if (JfrRecorder::is_recording()) {
   131     if (t->is_Java_thread()) {
   138     if (t->is_Java_thread() && !tl->is_excluded()) {
   132       send_java_thread_end_events(tl->thread_id(), (JavaThread*)t);
   139       send_java_thread_end_events(tl->thread_id(), (JavaThread*)t);
   133     }
   140     }
   134   }
   141   }
   135   release(tl, Thread::current()); // because it could be that Thread::current() != t
   142   release(tl, Thread::current()); // because it could be that Thread::current() != t
   136 }
   143 }
   137 
   144 
       
   145 static JfrBuffer* acquire_buffer(bool excluded) {
       
   146   JfrBuffer* const buffer = JfrStorage::acquire_thread_local(Thread::current());
       
   147   if (buffer != NULL && excluded) {
       
   148     buffer->set_excluded();
       
   149   }
       
   150   return buffer;
       
   151 }
       
   152 
   138 JfrBuffer* JfrThreadLocal::install_native_buffer() const {
   153 JfrBuffer* JfrThreadLocal::install_native_buffer() const {
   139   assert(!has_native_buffer(), "invariant");
   154   assert(!has_native_buffer(), "invariant");
   140   _native_buffer = JfrStorage::acquire_thread_local(Thread::current());
   155   _native_buffer = acquire_buffer(_excluded);
   141   return _native_buffer;
   156   return _native_buffer;
   142 }
   157 }
   143 
   158 
   144 JfrBuffer* JfrThreadLocal::install_java_buffer() const {
   159 JfrBuffer* JfrThreadLocal::install_java_buffer() const {
   145   assert(!has_java_buffer(), "invariant");
   160   assert(!has_java_buffer(), "invariant");
   146   assert(!has_java_event_writer(), "invariant");
   161   assert(!has_java_event_writer(), "invariant");
   147   _java_buffer = JfrStorage::acquire_thread_local(Thread::current());
   162   _java_buffer = acquire_buffer(_excluded);
   148   return _java_buffer;
   163   return _java_buffer;
   149 }
   164 }
   150 
   165 
   151 JfrStackFrame* JfrThreadLocal::install_stackframes() const {
   166 JfrStackFrame* JfrThreadLocal::install_stackframes() const {
   152   assert(_stackframes == NULL, "invariant");
   167   assert(_stackframes == NULL, "invariant");
   161 }
   176 }
   162 
   177 
   163 ByteSize JfrThreadLocal::java_event_writer_offset() {
   178 ByteSize JfrThreadLocal::java_event_writer_offset() {
   164   return in_ByteSize(offset_of(JfrThreadLocal, _java_event_writer));
   179   return in_ByteSize(offset_of(JfrThreadLocal, _java_event_writer));
   165 }
   180 }
       
   181 
       
   182 void JfrThreadLocal::exclude(Thread* t) {
       
   183   assert(t != NULL, "invariant");
       
   184   t->jfr_thread_local()->_excluded = true;
       
   185 }
       
   186 
       
   187 void JfrThreadLocal::include(Thread* t) {
       
   188   assert(t != NULL, "invariant");
       
   189   t->jfr_thread_local()->_excluded = false;
       
   190 }