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