1 /* 2 * Copyright (c) 2012, 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/jfrRecorder.hpp" 27 #include "jfr/periodic/sampling/jfrCallTrace.hpp" 28 #include "jfr/periodic/sampling/jfrThreadSampler.hpp" 29 #include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp" 30 #include "jfr/recorder/access/jfrOptionSet.hpp" 31 #include "jfr/utilities/jfrTraceTime.hpp" 32 #include "logging/log.hpp" 33 #include "runtime/frame.inline.hpp" 34 #include "runtime/os.hpp" 35 #include "runtime/thread.inline.hpp" 36 #include "runtime/threadSMR.hpp" 37 #include "trace/tracing.hpp" 38 #include "tracefiles/traceEventIds.hpp" 39 40 static bool in_java_sample(JavaThread* thread) { 41 switch (thread->thread_state()) { 42 case _thread_new: 43 case _thread_uninitialized: 44 case _thread_new_trans: 45 case _thread_in_vm_trans: 46 case _thread_blocked_trans: 47 case _thread_in_native_trans: 48 case _thread_blocked: 49 case _thread_in_vm: 50 case _thread_in_native: 51 case _thread_in_Java_trans: 52 break; 53 case _thread_in_Java: 54 return true; 55 default: 56 ShouldNotReachHere(); 57 break; 58 } 59 return false; 60 } 61 62 static bool in_native_sample(JavaThread* thread) { 63 switch (thread->thread_state()) { 64 case _thread_new: 65 case _thread_uninitialized: 66 case _thread_new_trans: 67 case _thread_blocked_trans: 68 case _thread_blocked: 69 case _thread_in_vm: 70 case _thread_in_vm_trans: 71 case _thread_in_Java_trans: 72 case _thread_in_Java: 73 case _thread_in_native_trans: 74 break; 75 case _thread_in_native: 76 return true; 77 default: 78 ShouldNotReachHere(); 79 break; 80 } 81 return false; 82 } 83 84 class JfrThreadSampleClosure { 85 public: 86 JfrThreadSampleClosure(EventExecutionSample* events, EventNativeMethodSample* events_native); 87 ~JfrThreadSampleClosure() {} 88 EventExecutionSample* next_event() { return &_events[_added_java++]; } 89 EventNativeMethodSample* next_event_native() { return &_events_native[_added_native++]; } 90 void commit_events(); 91 int added() const { return _added_java; } 92 JfrSampleType do_sample_thread(JavaThread* thread, JfrStackFrame* frames, u4 max_frames, bool java_sample, bool native_sample); 93 int java_entries() { return _added_java; } 94 int native_entries() { return _added_native; } 95 96 private: 97 bool sample_thread_in_java(JavaThread* thread, JfrStackFrame* frames, u4 max_frames); 98 bool sample_thread_in_native(JavaThread* thread, JfrStackFrame* frames, u4 max_frames); 99 EventExecutionSample* _events; 100 EventNativeMethodSample* _events_native; 101 Thread* _self; 102 int _added_java; 103 int _added_native; 104 }; 105 106 class OSThreadSampler : public os::SuspendedThreadTask { 107 public: 108 OSThreadSampler(JavaThread* thread, 109 JfrThreadSampleClosure& closure, 110 JfrStackFrame *frames, 111 u4 max_frames) : os::SuspendedThreadTask((Thread*)thread), 112 _success(false), 113 _stacktrace(frames, max_frames), 114 _closure(closure), 115 _suspend_time(0) {} 116 117 void take_sample(); 118 void do_task(const os::SuspendedThreadTaskContext& context); 119 void protected_task(const os::SuspendedThreadTaskContext& context); 120 bool success() const { return _success; } 121 const JfrStackTrace& stacktrace() const { return _stacktrace; } 122 123 private: 124 bool _success; 125 JfrStackTrace _stacktrace; 126 JfrThreadSampleClosure& _closure; 127 JfrTraceTime _suspend_time; 128 }; 129 130 class OSThreadSamplerCallback : public os::CrashProtectionCallback { 131 public: 132 OSThreadSamplerCallback(OSThreadSampler& sampler, const os::SuspendedThreadTaskContext &context) : 133 _sampler(sampler), _context(context) { 134 } 135 virtual void call() { 136 _sampler.protected_task(_context); 137 } 138 private: 139 OSThreadSampler& _sampler; 140 const os::SuspendedThreadTaskContext& _context; 141 }; 142 143 void OSThreadSampler::do_task(const os::SuspendedThreadTaskContext& context) { 144 #ifndef ASSERT 145 guarantee(JfrOptionSet::sample_protection(), "Sample Protection should be on in product builds"); 146 #endif 147 assert(0 == _suspend_time, "already timestamped!"); 148 _suspend_time = JfrTraceTime::now(); 149 150 if (JfrOptionSet::sample_protection()) { 151 OSThreadSamplerCallback cb(*this, context); 152 os::ThreadCrashProtection crash_protection; 153 if (!crash_protection.call(cb)) { 154 log_error(jfr)("Thread method sampler crashed"); 155 } 156 } else { 157 protected_task(context); 158 } 159 } 160 161 /* 162 * From this method and down the call tree we attempt to protect against crashes 163 * using a signal handler / __try block. Don't take locks, rely on destructors or 164 * leave memory (in case of signal / exception) in an inconsistent state. */ 165 void OSThreadSampler::protected_task(const os::SuspendedThreadTaskContext& context) { 166 JavaThread* jth = (JavaThread*)context.thread(); 167 // Skip sample if we signaled a thread that moved to other state 168 if (!in_java_sample(jth)) { 169 return; 170 } 171 JfrGetCallTrace trace(true, jth); 172 frame topframe; 173 if (trace.get_topframe(context.ucontext(), topframe)) { 174 if (_stacktrace.record_thread(*jth, topframe)) { 175 /* If we managed to get a topframe and a stacktrace, create an event 176 * and put it into our array. We can't call Jfr::_stacktraces.add() 177 * here since it would allocate memory using malloc. Doing so while 178 * the stopped thread is inside malloc would deadlock. */ 179 _success = true; 180 EventExecutionSample *ev = _closure.next_event(); 181 ev->set_starttime(_suspend_time); 182 ev->set_endtime(JfrTraceTime(1)); // fake to not take an end time 183 ev->set_sampledThread(THREAD_TRACE_ID(jth)); 184 ev->set_state(java_lang_Thread::get_thread_status(jth->threadObj())); 185 } 186 } 187 } 188 189 void OSThreadSampler::take_sample() { 190 run(); 191 } 192 193 class JfrNativeSamplerCallback : public os::CrashProtectionCallback { 194 public: 195 JfrNativeSamplerCallback(JfrThreadSampleClosure& closure, JavaThread* jt, JfrStackFrame* frames, u4 max_frames) : 196 _closure(closure), _jt(jt), _stacktrace(frames, max_frames), _success(false) { 197 } 198 virtual void call(); 199 bool success() { return _success; } 200 JfrStackTrace& stacktrace() { return _stacktrace; } 201 202 private: 203 JfrThreadSampleClosure& _closure; 204 JavaThread* _jt; 205 JfrStackTrace _stacktrace; 206 bool _success; 207 }; 208 209 static void write_native_event(JfrThreadSampleClosure& closure, JavaThread* jt) { 210 EventNativeMethodSample *ev = closure.next_event_native(); 211 ev->set_starttime(JfrTraceTime(0)); 212 ev->set_sampledThread(THREAD_TRACE_ID(jt)); 213 ev->set_state(java_lang_Thread::get_thread_status(jt->threadObj())); 214 } 215 216 void JfrNativeSamplerCallback::call() { 217 // When a thread is only attach it will be native without a last java frame 218 if (!_jt->has_last_Java_frame()) { 219 return; 220 } 221 222 frame topframe = _jt->last_frame(); 223 frame first_java_frame; 224 Method* method = NULL; 225 JfrGetCallTrace gct(false, _jt); 226 if (!gct.find_top_frame(topframe, &method, first_java_frame)) { 227 return; 228 } 229 if (method == NULL) { 230 return; 231 } 232 topframe = first_java_frame; 233 _success = _stacktrace.record_thread(*_jt, topframe); 234 if (_success) { 235 write_native_event(_closure, _jt); 236 } 237 } 238 239 bool JfrThreadSampleClosure::sample_thread_in_java(JavaThread* thread, JfrStackFrame* frames, u4 max_frames) { 240 OSThreadSampler sampler(thread, *this, frames, max_frames); 241 sampler.take_sample(); 242 /* We don't want to allocate any memory using malloc/etc while the thread 243 * is stopped, so everything is stored in stack allocated memory until this 244 * point where the thread has been resumed again, if the sampling was a success 245 * we need to store the stacktrace in the stacktrace repository and update 246 * the event with the id that was returned. */ 247 if (!sampler.success()) { 248 return false; 249 } 250 EventExecutionSample *event = &_events[_added_java - 1]; 251 traceid id = JfrStackTraceRepository::add(sampler.stacktrace()); 252 assert(id != 0, "Stacktrace id should not be 0"); 253 event->set_stackTrace(id); 254 return true; 255 } 256 257 bool JfrThreadSampleClosure::sample_thread_in_native(JavaThread* thread, JfrStackFrame* frames, u4 max_frames) { 258 JfrNativeSamplerCallback cb(*this, thread, frames, max_frames); 259 if (JfrOptionSet::sample_protection()) { 260 os::ThreadCrashProtection crash_protection; 261 if (!crash_protection.call(cb)) { 262 log_error(jfr)("Thread method sampler crashed for native"); 263 } 264 } else { 265 cb.call(); 266 } 267 if (!cb.success()) { 268 return false; 269 } 270 EventNativeMethodSample *event = &_events_native[_added_native - 1]; 271 traceid id = JfrStackTraceRepository::add(cb.stacktrace()); 272 assert(id != 0, "Stacktrace id should not be 0"); 273 event->set_stackTrace(id); 274 return true; 275 } 276 277 void JfrThreadSampleClosure::commit_events() { 278 for (int i = 0; i < _added_java; ++i) { 279 _events[i].commit(); 280 } 281 for (int i = 0; i < _added_native; ++i) { 282 _events_native[i].commit(); 283 } 284 } 285 286 JfrThreadSampleClosure::JfrThreadSampleClosure(EventExecutionSample* events, EventNativeMethodSample* events_native) : 287 _events(events), 288 _events_native(events_native), 289 _self(Thread::current()), 290 _added_java(0), 291 _added_native(0) { 292 } 293 294 static void clear_transition_block(JavaThread* jt) { 295 jt->clear_trace_flag(); 296 JfrThreadData* jtd = jt->trace_data(); 297 if (jtd->is_trace_block()) { 298 MutexLockerEx ml(JfrThreadSampler::transition_block(), Mutex::_no_safepoint_check_flag); 299 JfrThreadSampler::transition_block()->notify_all(); 300 } 301 } 302 303 JfrSampleType JfrThreadSampleClosure::do_sample_thread(JavaThread* thread, JfrStackFrame* frames, u4 max_frames, bool java_sample, bool native_sample) { 304 assert(Threads_lock->owned_by_self(), "Holding the thread table lock."); 305 if (thread->is_hidden_from_external_view()) { 306 return NO_SAMPLE; 307 } 308 if (thread->in_deopt_handler()) { 309 return NO_SAMPLE; 310 } 311 JfrSampleType ret = NO_SAMPLE; 312 thread->set_trace_flag(); 313 if (!UseMembar) { 314 os::serialize_thread_states(); 315 } 316 if (in_java_sample(thread) && java_sample) { 317 ret = sample_thread_in_java(thread, frames, max_frames) ? JAVA_SAMPLE : NO_SAMPLE; 318 } else if (in_native_sample(thread) && native_sample) { 319 ret = sample_thread_in_native(thread, frames, max_frames) ? NATIVE_SAMPLE : NO_SAMPLE; 320 } 321 clear_transition_block(thread); 322 return ret; 323 } 324 325 Monitor* JfrThreadSampler::_transition_block_lock = new Monitor(Mutex::leaf, "Trace block", true, Monitor::_safepoint_check_never); 326 327 JfrThreadSampler::JfrThreadSampler(size_t interval_java, size_t interval_native, u4 max_frames) : 328 _frames(JfrCHeapObj::new_array<JfrStackFrame>(max_frames)), 329 _last_thread_java(NULL), 330 _last_thread_native(NULL), 331 _interval_java(interval_java), 332 _interval_native(interval_native), 333 _cur_index(-1), 334 _max_frames(max_frames), 335 _should_terminate(false) { 336 } 337 338 JfrThreadSampler::~JfrThreadSampler() { 339 JfrCHeapObj::free(_frames, sizeof(JfrStackFrame) * _max_frames); 340 } 341 342 void JfrThreadSampler::on_javathread_suspend(JavaThread* thread) { 343 JfrThreadData* jtd = thread->trace_data(); 344 jtd->set_trace_block(); 345 { 346 MutexLockerEx ml(transition_block(), Mutex::_no_safepoint_check_flag); 347 while (thread->is_trace_suspend()) { 348 transition_block()->wait(true); 349 } 350 jtd->clear_trace_block(); 351 } 352 } 353 354 JavaThread* JfrThreadSampler::next_thread(ThreadsList* t_list, JavaThread* first_sampled, JavaThread* current) { 355 assert(Threads_lock->owned_by_self(), "Holding the thread table lock."); 356 if (current == NULL) { 357 _cur_index = 0; 358 return t_list->thread_at(_cur_index); 359 } 360 361 if (_cur_index == -1 || t_list->thread_at(_cur_index) != current) { 362 // 'current' is not at '_cur_index' so find it: 363 _cur_index = t_list->find_index_of_JavaThread(current); 364 assert(_cur_index != -1, "current JavaThread should be findable."); 365 } 366 _cur_index++; 367 368 JavaThread* next = NULL; 369 // wrap 370 if ((uint)_cur_index >= t_list->length()) { 371 _cur_index = 0; 372 } 373 next = t_list->thread_at(_cur_index); 374 375 // sample wrap 376 if (next == first_sampled) { 377 return NULL; 378 } 379 return next; 380 } 381 382 void JfrThreadSampler::enroll() { 383 if (os::create_thread(this, os::os_thread)) { 384 os::start_thread(this); 385 } else { 386 log_error(jfr)("Failed to create thread for thread sampling"); 387 } 388 } 389 390 void JfrThreadSampler::disenroll() { 391 _should_terminate = true; 392 } 393 394 static jlong get_monotonic_ms() { 395 return os::javaTimeNanos() / 1000000; 396 } 397 398 void JfrThreadSampler::run() { 399 jlong last_java_ms = get_monotonic_ms(); 400 jlong last_native_ms = last_java_ms; 401 while (!_should_terminate) { 402 jlong java_interval = _interval_java == 0 ? max_jlong : MAX2<jlong>(_interval_java, 10); 403 jlong native_interval = _interval_native == 0 ? max_jlong : MAX2<jlong>(_interval_native, 10); 404 405 jlong now_ms = get_monotonic_ms(); 406 407 jlong next_j = java_interval + last_java_ms - now_ms; 408 jlong next_n = native_interval + last_native_ms - now_ms; 409 410 jlong sleep_to_next = MIN2<jlong>(next_j, next_n); 411 412 if (sleep_to_next > 0) { 413 os::naked_short_sleep(sleep_to_next); 414 } 415 416 if ((next_j - sleep_to_next) <= 0) { 417 task_stacktrace(JAVA_SAMPLE, &_last_thread_java); 418 last_java_ms = get_monotonic_ms(); 419 } 420 if ((next_n - sleep_to_next) <= 0) { 421 task_stacktrace(NATIVE_SAMPLE, &_last_thread_native); 422 last_native_ms = get_monotonic_ms(); 423 } 424 } 425 delete this; 426 } 427 428 static const int MAX_NR_OF_SAMPLES = 5; 429 430 void JfrThreadSampler::task_stacktrace(JfrSampleType type, JavaThread** last_thread) { 431 ResourceMark rm; 432 EventExecutionSample samples[MAX_NR_OF_SAMPLES]; 433 EventNativeMethodSample samples_native[MAX_NR_OF_SAMPLES]; 434 JfrThreadSampleClosure sample_task(samples, samples_native); 435 436 int num_samples = 0; 437 { 438 elapsedTimer sample_time; 439 sample_time.start(); 440 441 { 442 MonitorLockerEx tlock(Threads_lock, Mutex::_allow_vm_block_flag); 443 ThreadsListHandle tlh; 444 JavaThread* current = tlh.includes(*last_thread) ? *last_thread : NULL; 445 JavaThread* start = NULL; 446 447 while (num_samples < MAX_NR_OF_SAMPLES) { 448 current = next_thread(tlh.list(), start, current); 449 if (current == NULL) { 450 break; 451 } 452 if (start == NULL) { 453 start = current; // remember thread where we started sampling 454 } 455 if (current->is_Compiler_thread()) { 456 continue; 457 } 458 *last_thread = current; // remember thread we last sampled 459 JfrSampleType ret = sample_task.do_sample_thread(current, _frames, _max_frames, type == JAVA_SAMPLE, type == NATIVE_SAMPLE); 460 switch (type) { 461 case JAVA_SAMPLE: 462 case NATIVE_SAMPLE: 463 ++num_samples; 464 break; 465 default: 466 break; 467 } 468 } 469 } 470 sample_time.stop(); 471 log_trace(jfr)("JFR thread sampling done in %3.7f secs with %d java %d native samples", 472 sample_time.seconds(), sample_task.java_entries(), sample_task.native_entries()); 473 } 474 if (num_samples>0) { 475 sample_task.commit_events(); 476 } 477 } 478 479 static JfrThreadSampling* _instance = NULL; 480 481 JfrThreadSampling& JfrThreadSampling::instance() { 482 return *_instance; 483 } 484 485 JfrThreadSampling* JfrThreadSampling::create() { 486 assert(_instance == NULL, "invariant"); 487 _instance = new JfrThreadSampling(); 488 return _instance; 489 } 490 491 void JfrThreadSampling::destroy() { 492 if (_instance != NULL) { 493 delete _instance; 494 _instance = NULL; 495 } 496 } 497 498 JfrThreadSampling::JfrThreadSampling() : _sampler(NULL) {} 499 500 JfrThreadSampling::~JfrThreadSampling() { 501 log_info(jfr)("Disrolling thread sampler"); 502 stop_sampler(); 503 } 504 505 static void log(size_t interval_java, size_t interval_native) { 506 log_info(jfr)("Updated thread sampler for java: " SIZE_FORMAT" ms, native " SIZE_FORMAT " ms", interval_java, interval_native); 507 } 508 509 void JfrThreadSampling::start_sampler(size_t interval_java, size_t interval_native) { 510 assert(_sampler == NULL, "invariant"); 511 log_info(jfr)("Enrolling thread sampler"); 512 _sampler = new JfrThreadSampler(interval_java, interval_native, JfrOptionSet::stackdepth()); 513 _sampler->enroll(); 514 } 515 516 void JfrThreadSampling::stop_sampler() { 517 if (_sampler != NULL) { 518 _sampler->disenroll(); 519 _sampler = NULL; 520 } 521 } 522 523 void JfrThreadSampling::set_sampling_interval(bool java_interval, size_t period) { 524 size_t interval_java = 0; 525 size_t interval_native = 0; 526 if (_sampler != NULL) { 527 interval_java = _sampler->get_java_interval(); 528 interval_native = _sampler->get_native_interval(); 529 } 530 531 if (java_interval) { 532 interval_java = period; 533 } else { 534 interval_native = period; 535 } 536 537 if (interval_java > 0 || interval_native > 0) { 538 if (_sampler == NULL) { 539 start_sampler(interval_java, interval_native); 540 } else { 541 _sampler->set_java_interval(interval_java); 542 _sampler->set_native_interval(interval_native); 543 } 544 assert(_sampler != NULL, "invariant"); 545 log(interval_java, interval_native); 546 } else if (_sampler != NULL) { 547 log_info(jfr)("Disrolling thread sampler"); 548 stop_sampler(); 549 } 550 } 551 552 void JfrThreadSampling::set_java_sample_interval(size_t period) { 553 if (_instance == NULL && 0 == period) { 554 return; 555 } 556 instance().set_sampling_interval(true, period); 557 } 558 559 void JfrThreadSampling::set_native_sample_interval(size_t period) { 560 if (_instance == NULL && 0 == period) { 561 return; 562 } 563 instance().set_sampling_interval(false, period); 564 } 565