src/share/vm/runtime/vmThread.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6680
78bbf4d43a14
parent 0
f90c822e73f8
child 7535
7ae4e26cb1e0
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 1998, 2014, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 *
aoqi@0 23 */
aoqi@0 24
aoqi@0 25 #include "precompiled.hpp"
aoqi@0 26 #include "compiler/compileBroker.hpp"
aoqi@0 27 #include "gc_interface/collectedHeap.hpp"
aoqi@0 28 #include "memory/resourceArea.hpp"
aoqi@0 29 #include "oops/method.hpp"
aoqi@0 30 #include "oops/oop.inline.hpp"
aoqi@0 31 #include "runtime/interfaceSupport.hpp"
aoqi@0 32 #include "runtime/mutexLocker.hpp"
aoqi@0 33 #include "runtime/os.hpp"
aoqi@0 34 #include "runtime/thread.inline.hpp"
aoqi@0 35 #include "runtime/vmThread.hpp"
aoqi@0 36 #include "runtime/vm_operations.hpp"
aoqi@0 37 #include "services/runtimeService.hpp"
aoqi@0 38 #include "trace/tracing.hpp"
aoqi@0 39 #include "utilities/dtrace.hpp"
aoqi@0 40 #include "utilities/events.hpp"
aoqi@0 41 #include "utilities/xmlstream.hpp"
aoqi@0 42
aoqi@0 43 #ifndef USDT2
aoqi@0 44 HS_DTRACE_PROBE_DECL3(hotspot, vmops__request, char *, uintptr_t, int);
aoqi@0 45 HS_DTRACE_PROBE_DECL3(hotspot, vmops__begin, char *, uintptr_t, int);
aoqi@0 46 HS_DTRACE_PROBE_DECL3(hotspot, vmops__end, char *, uintptr_t, int);
aoqi@0 47 #endif /* !USDT2 */
aoqi@0 48
aoqi@0 49 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
aoqi@0 50
aoqi@0 51 // Dummy VM operation to act as first element in our circular double-linked list
aoqi@0 52 class VM_Dummy: public VM_Operation {
aoqi@0 53 VMOp_Type type() const { return VMOp_Dummy; }
aoqi@0 54 void doit() {};
aoqi@0 55 };
aoqi@0 56
aoqi@0 57 VMOperationQueue::VMOperationQueue() {
aoqi@0 58 // The queue is a circular doubled-linked list, which always contains
aoqi@0 59 // one element (i.e., one element means empty).
aoqi@0 60 for(int i = 0; i < nof_priorities; i++) {
aoqi@0 61 _queue_length[i] = 0;
aoqi@0 62 _queue_counter = 0;
aoqi@0 63 _queue[i] = new VM_Dummy();
aoqi@0 64 _queue[i]->set_next(_queue[i]);
aoqi@0 65 _queue[i]->set_prev(_queue[i]);
aoqi@0 66 }
aoqi@0 67 _drain_list = NULL;
aoqi@0 68 }
aoqi@0 69
aoqi@0 70
aoqi@0 71 bool VMOperationQueue::queue_empty(int prio) {
aoqi@0 72 // It is empty if there is exactly one element
aoqi@0 73 bool empty = (_queue[prio] == _queue[prio]->next());
aoqi@0 74 assert( (_queue_length[prio] == 0 && empty) ||
aoqi@0 75 (_queue_length[prio] > 0 && !empty), "sanity check");
aoqi@0 76 return _queue_length[prio] == 0;
aoqi@0 77 }
aoqi@0 78
aoqi@0 79 // Inserts an element to the right of the q element
aoqi@0 80 void VMOperationQueue::insert(VM_Operation* q, VM_Operation* n) {
aoqi@0 81 assert(q->next()->prev() == q && q->prev()->next() == q, "sanity check");
aoqi@0 82 n->set_prev(q);
aoqi@0 83 n->set_next(q->next());
aoqi@0 84 q->next()->set_prev(n);
aoqi@0 85 q->set_next(n);
aoqi@0 86 }
aoqi@0 87
aoqi@0 88 void VMOperationQueue::queue_add_front(int prio, VM_Operation *op) {
aoqi@0 89 _queue_length[prio]++;
aoqi@0 90 insert(_queue[prio]->next(), op);
aoqi@0 91 }
aoqi@0 92
aoqi@0 93 void VMOperationQueue::queue_add_back(int prio, VM_Operation *op) {
aoqi@0 94 _queue_length[prio]++;
aoqi@0 95 insert(_queue[prio]->prev(), op);
aoqi@0 96 }
aoqi@0 97
aoqi@0 98
aoqi@0 99 void VMOperationQueue::unlink(VM_Operation* q) {
aoqi@0 100 assert(q->next()->prev() == q && q->prev()->next() == q, "sanity check");
aoqi@0 101 q->prev()->set_next(q->next());
aoqi@0 102 q->next()->set_prev(q->prev());
aoqi@0 103 }
aoqi@0 104
aoqi@0 105 VM_Operation* VMOperationQueue::queue_remove_front(int prio) {
aoqi@0 106 if (queue_empty(prio)) return NULL;
aoqi@0 107 assert(_queue_length[prio] >= 0, "sanity check");
aoqi@0 108 _queue_length[prio]--;
aoqi@0 109 VM_Operation* r = _queue[prio]->next();
aoqi@0 110 assert(r != _queue[prio], "cannot remove base element");
aoqi@0 111 unlink(r);
aoqi@0 112 return r;
aoqi@0 113 }
aoqi@0 114
aoqi@0 115 VM_Operation* VMOperationQueue::queue_drain(int prio) {
aoqi@0 116 if (queue_empty(prio)) return NULL;
aoqi@0 117 DEBUG_ONLY(int length = _queue_length[prio];);
aoqi@0 118 assert(length >= 0, "sanity check");
aoqi@0 119 _queue_length[prio] = 0;
aoqi@0 120 VM_Operation* r = _queue[prio]->next();
aoqi@0 121 assert(r != _queue[prio], "cannot remove base element");
aoqi@0 122 // remove links to base element from head and tail
aoqi@0 123 r->set_prev(NULL);
aoqi@0 124 _queue[prio]->prev()->set_next(NULL);
aoqi@0 125 // restore queue to empty state
aoqi@0 126 _queue[prio]->set_next(_queue[prio]);
aoqi@0 127 _queue[prio]->set_prev(_queue[prio]);
aoqi@0 128 assert(queue_empty(prio), "drain corrupted queue");
aoqi@0 129 #ifdef ASSERT
aoqi@0 130 int len = 0;
aoqi@0 131 VM_Operation* cur;
aoqi@0 132 for(cur = r; cur != NULL; cur=cur->next()) len++;
aoqi@0 133 assert(len == length, "drain lost some ops");
aoqi@0 134 #endif
aoqi@0 135 return r;
aoqi@0 136 }
aoqi@0 137
aoqi@0 138 void VMOperationQueue::queue_oops_do(int queue, OopClosure* f) {
aoqi@0 139 VM_Operation* cur = _queue[queue];
aoqi@0 140 cur = cur->next();
aoqi@0 141 while (cur != _queue[queue]) {
aoqi@0 142 cur->oops_do(f);
aoqi@0 143 cur = cur->next();
aoqi@0 144 }
aoqi@0 145 }
aoqi@0 146
aoqi@0 147 void VMOperationQueue::drain_list_oops_do(OopClosure* f) {
aoqi@0 148 VM_Operation* cur = _drain_list;
aoqi@0 149 while (cur != NULL) {
aoqi@0 150 cur->oops_do(f);
aoqi@0 151 cur = cur->next();
aoqi@0 152 }
aoqi@0 153 }
aoqi@0 154
aoqi@0 155 //-----------------------------------------------------------------
aoqi@0 156 // High-level interface
aoqi@0 157 bool VMOperationQueue::add(VM_Operation *op) {
aoqi@0 158
aoqi@0 159 #ifndef USDT2
aoqi@0 160 HS_DTRACE_PROBE3(hotspot, vmops__request, op->name(), strlen(op->name()),
aoqi@0 161 op->evaluation_mode());
aoqi@0 162 #else /* USDT2 */
aoqi@0 163 HOTSPOT_VMOPS_REQUEST(
aoqi@0 164 (char *) op->name(), strlen(op->name()),
aoqi@0 165 op->evaluation_mode());
aoqi@0 166 #endif /* USDT2 */
aoqi@0 167
aoqi@0 168 // Encapsulates VM queue policy. Currently, that
aoqi@0 169 // only involves putting them on the right list
aoqi@0 170 if (op->evaluate_at_safepoint()) {
aoqi@0 171 queue_add_back(SafepointPriority, op);
aoqi@0 172 return true;
aoqi@0 173 }
aoqi@0 174
aoqi@0 175 queue_add_back(MediumPriority, op);
aoqi@0 176 return true;
aoqi@0 177 }
aoqi@0 178
aoqi@0 179 VM_Operation* VMOperationQueue::remove_next() {
aoqi@0 180 // Assuming VMOperation queue is two-level priority queue. If there are
aoqi@0 181 // more than two priorities, we need a different scheduling algorithm.
aoqi@0 182 assert(SafepointPriority == 0 && MediumPriority == 1 && nof_priorities == 2,
aoqi@0 183 "current algorithm does not work");
aoqi@0 184
aoqi@0 185 // simple counter based scheduling to prevent starvation of lower priority
aoqi@0 186 // queue. -- see 4390175
aoqi@0 187 int high_prio, low_prio;
aoqi@0 188 if (_queue_counter++ < 10) {
aoqi@0 189 high_prio = SafepointPriority;
aoqi@0 190 low_prio = MediumPriority;
aoqi@0 191 } else {
aoqi@0 192 _queue_counter = 0;
aoqi@0 193 high_prio = MediumPriority;
aoqi@0 194 low_prio = SafepointPriority;
aoqi@0 195 }
aoqi@0 196
aoqi@0 197 return queue_remove_front(queue_empty(high_prio) ? low_prio : high_prio);
aoqi@0 198 }
aoqi@0 199
aoqi@0 200 void VMOperationQueue::oops_do(OopClosure* f) {
aoqi@0 201 for(int i = 0; i < nof_priorities; i++) {
aoqi@0 202 queue_oops_do(i, f);
aoqi@0 203 }
aoqi@0 204 drain_list_oops_do(f);
aoqi@0 205 }
aoqi@0 206
aoqi@0 207
aoqi@0 208 //------------------------------------------------------------------------------------------------------------------
aoqi@0 209 // Implementation of VMThread stuff
aoqi@0 210
aoqi@0 211 bool VMThread::_should_terminate = false;
aoqi@0 212 bool VMThread::_terminated = false;
aoqi@0 213 Monitor* VMThread::_terminate_lock = NULL;
aoqi@0 214 VMThread* VMThread::_vm_thread = NULL;
aoqi@0 215 VM_Operation* VMThread::_cur_vm_operation = NULL;
aoqi@0 216 VMOperationQueue* VMThread::_vm_queue = NULL;
aoqi@0 217 PerfCounter* VMThread::_perf_accumulated_vm_operation_time = NULL;
aoqi@0 218
aoqi@0 219
aoqi@0 220 void VMThread::create() {
aoqi@0 221 assert(vm_thread() == NULL, "we can only allocate one VMThread");
aoqi@0 222 _vm_thread = new VMThread();
aoqi@0 223
aoqi@0 224 // Create VM operation queue
aoqi@0 225 _vm_queue = new VMOperationQueue();
aoqi@0 226 guarantee(_vm_queue != NULL, "just checking");
aoqi@0 227
aoqi@0 228 _terminate_lock = new Monitor(Mutex::safepoint, "VMThread::_terminate_lock", true);
aoqi@0 229
aoqi@0 230 if (UsePerfData) {
aoqi@0 231 // jvmstat performance counters
aoqi@0 232 Thread* THREAD = Thread::current();
aoqi@0 233 _perf_accumulated_vm_operation_time =
aoqi@0 234 PerfDataManager::create_counter(SUN_THREADS, "vmOperationTime",
aoqi@0 235 PerfData::U_Ticks, CHECK);
aoqi@0 236 }
aoqi@0 237 }
aoqi@0 238
aoqi@0 239
aoqi@0 240 VMThread::VMThread() : NamedThread() {
aoqi@0 241 set_name("VM Thread");
aoqi@0 242 }
aoqi@0 243
aoqi@0 244 void VMThread::destroy() {
aoqi@0 245 if (_vm_thread != NULL) {
aoqi@0 246 delete _vm_thread;
aoqi@0 247 _vm_thread = NULL; // VM thread is gone
aoqi@0 248 }
aoqi@0 249 }
aoqi@0 250
aoqi@0 251 void VMThread::run() {
aoqi@0 252 assert(this == vm_thread(), "check");
aoqi@0 253
aoqi@0 254 this->initialize_thread_local_storage();
aoqi@0 255 this->record_stack_base_and_size();
aoqi@0 256 // Notify_lock wait checks on active_handles() to rewait in
aoqi@0 257 // case of spurious wakeup, it should wait on the last
aoqi@0 258 // value set prior to the notify
aoqi@0 259 this->set_active_handles(JNIHandleBlock::allocate_block());
aoqi@0 260
aoqi@0 261 {
aoqi@0 262 MutexLocker ml(Notify_lock);
aoqi@0 263 Notify_lock->notify();
aoqi@0 264 }
aoqi@0 265 // Notify_lock is destroyed by Threads::create_vm()
aoqi@0 266
aoqi@0 267 int prio = (VMThreadPriority == -1)
aoqi@0 268 ? os::java_to_os_priority[NearMaxPriority]
aoqi@0 269 : VMThreadPriority;
aoqi@0 270 // Note that I cannot call os::set_priority because it expects Java
aoqi@0 271 // priorities and I am *explicitly* using OS priorities so that it's
aoqi@0 272 // possible to set the VM thread priority higher than any Java thread.
aoqi@0 273 os::set_native_priority( this, prio );
aoqi@0 274
aoqi@0 275 // Wait for VM_Operations until termination
aoqi@0 276 this->loop();
aoqi@0 277
aoqi@0 278 // Note the intention to exit before safepointing.
aoqi@0 279 // 6295565 This has the effect of waiting for any large tty
aoqi@0 280 // outputs to finish.
aoqi@0 281 if (xtty != NULL) {
aoqi@0 282 ttyLocker ttyl;
aoqi@0 283 xtty->begin_elem("destroy_vm");
aoqi@0 284 xtty->stamp();
aoqi@0 285 xtty->end_elem();
aoqi@0 286 assert(should_terminate(), "termination flag must be set");
aoqi@0 287 }
aoqi@0 288
aoqi@0 289 // 4526887 let VM thread exit at Safepoint
aoqi@0 290 SafepointSynchronize::begin();
aoqi@0 291
aoqi@0 292 if (VerifyBeforeExit) {
aoqi@0 293 HandleMark hm(VMThread::vm_thread());
aoqi@0 294 // Among other things, this ensures that Eden top is correct.
aoqi@0 295 Universe::heap()->prepare_for_verify();
aoqi@0 296 os::check_heap();
aoqi@0 297 // Silent verification so as not to pollute normal output,
aoqi@0 298 // unless we really asked for it.
aoqi@0 299 Universe::verify(!(PrintGCDetails || Verbose) || VerifySilently);
aoqi@0 300 }
aoqi@0 301
aoqi@0 302 CompileBroker::set_should_block();
aoqi@0 303
aoqi@0 304 // wait for threads (compiler threads or daemon threads) in the
aoqi@0 305 // _thread_in_native state to block.
aoqi@0 306 VM_Exit::wait_for_threads_in_native_to_block();
aoqi@0 307
aoqi@0 308 // signal other threads that VM process is gone
aoqi@0 309 {
aoqi@0 310 // Note: we must have the _no_safepoint_check_flag. Mutex::lock() allows
aoqi@0 311 // VM thread to enter any lock at Safepoint as long as its _owner is NULL.
aoqi@0 312 // If that happens after _terminate_lock->wait() has unset _owner
aoqi@0 313 // but before it actually drops the lock and waits, the notification below
aoqi@0 314 // may get lost and we will have a hang. To avoid this, we need to use
aoqi@0 315 // Mutex::lock_without_safepoint_check().
aoqi@0 316 MutexLockerEx ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 317 _terminated = true;
aoqi@0 318 _terminate_lock->notify();
aoqi@0 319 }
aoqi@0 320
aoqi@0 321 // Thread destructor usually does this.
aoqi@0 322 ThreadLocalStorage::set_thread(NULL);
aoqi@0 323
aoqi@0 324 // Deletion must be done synchronously by the JNI DestroyJavaVM thread
aoqi@0 325 // so that the VMThread deletion completes before the main thread frees
aoqi@0 326 // up the CodeHeap.
aoqi@0 327
aoqi@0 328 }
aoqi@0 329
aoqi@0 330
aoqi@0 331 // Notify the VMThread that the last non-daemon JavaThread has terminated,
aoqi@0 332 // and wait until operation is performed.
aoqi@0 333 void VMThread::wait_for_vm_thread_exit() {
aoqi@0 334 { MutexLocker mu(VMOperationQueue_lock);
aoqi@0 335 _should_terminate = true;
aoqi@0 336 VMOperationQueue_lock->notify();
aoqi@0 337 }
aoqi@0 338
aoqi@0 339 // Note: VM thread leaves at Safepoint. We are not stopped by Safepoint
aoqi@0 340 // because this thread has been removed from the threads list. But anything
aoqi@0 341 // that could get blocked by Safepoint should not be used after this point,
aoqi@0 342 // otherwise we will hang, since there is no one can end the safepoint.
aoqi@0 343
aoqi@0 344 // Wait until VM thread is terminated
aoqi@0 345 // Note: it should be OK to use Terminator_lock here. But this is called
aoqi@0 346 // at a very delicate time (VM shutdown) and we are operating in non- VM
aoqi@0 347 // thread at Safepoint. It's safer to not share lock with other threads.
aoqi@0 348 { MutexLockerEx ml(_terminate_lock, Mutex::_no_safepoint_check_flag);
aoqi@0 349 while(!VMThread::is_terminated()) {
aoqi@0 350 _terminate_lock->wait(Mutex::_no_safepoint_check_flag);
aoqi@0 351 }
aoqi@0 352 }
aoqi@0 353 }
aoqi@0 354
aoqi@0 355 void VMThread::print_on(outputStream* st) const {
aoqi@0 356 st->print("\"%s\" ", name());
aoqi@0 357 Thread::print_on(st);
aoqi@0 358 st->cr();
aoqi@0 359 }
aoqi@0 360
aoqi@0 361 void VMThread::evaluate_operation(VM_Operation* op) {
aoqi@0 362 ResourceMark rm;
aoqi@0 363
aoqi@0 364 {
aoqi@0 365 PerfTraceTime vm_op_timer(perf_accumulated_vm_operation_time());
aoqi@0 366 #ifndef USDT2
aoqi@0 367 HS_DTRACE_PROBE3(hotspot, vmops__begin, op->name(), strlen(op->name()),
aoqi@0 368 op->evaluation_mode());
aoqi@0 369 #else /* USDT2 */
aoqi@0 370 HOTSPOT_VMOPS_BEGIN(
aoqi@0 371 (char *) op->name(), strlen(op->name()),
aoqi@0 372 op->evaluation_mode());
aoqi@0 373 #endif /* USDT2 */
aoqi@0 374
aoqi@0 375 EventExecuteVMOperation event;
aoqi@0 376
aoqi@0 377 op->evaluate();
aoqi@0 378
aoqi@0 379 if (event.should_commit()) {
aoqi@0 380 bool is_concurrent = op->evaluate_concurrently();
aoqi@0 381 event.set_operation(op->type());
aoqi@0 382 event.set_safepoint(op->evaluate_at_safepoint());
aoqi@0 383 event.set_blocking(!is_concurrent);
aoqi@0 384 // Only write caller thread information for non-concurrent vm operations.
aoqi@0 385 // For concurrent vm operations, the thread id is set to 0 indicating thread is unknown.
aoqi@0 386 // This is because the caller thread could have exited already.
aoqi@0 387 event.set_caller(is_concurrent ? 0 : op->calling_thread()->osthread()->thread_id());
aoqi@0 388 event.commit();
aoqi@0 389 }
aoqi@0 390
aoqi@0 391 #ifndef USDT2
aoqi@0 392 HS_DTRACE_PROBE3(hotspot, vmops__end, op->name(), strlen(op->name()),
aoqi@0 393 op->evaluation_mode());
aoqi@0 394 #else /* USDT2 */
aoqi@0 395 HOTSPOT_VMOPS_END(
aoqi@0 396 (char *) op->name(), strlen(op->name()),
aoqi@0 397 op->evaluation_mode());
aoqi@0 398 #endif /* USDT2 */
aoqi@0 399 }
aoqi@0 400
aoqi@0 401 // Last access of info in _cur_vm_operation!
aoqi@0 402 bool c_heap_allocated = op->is_cheap_allocated();
aoqi@0 403
aoqi@0 404 // Mark as completed
aoqi@0 405 if (!op->evaluate_concurrently()) {
aoqi@0 406 op->calling_thread()->increment_vm_operation_completed_count();
aoqi@0 407 }
aoqi@0 408 // It is unsafe to access the _cur_vm_operation after the 'increment_vm_operation_completed_count' call,
aoqi@0 409 // since if it is stack allocated the calling thread might have deallocated
aoqi@0 410 if (c_heap_allocated) {
aoqi@0 411 delete _cur_vm_operation;
aoqi@0 412 }
aoqi@0 413 }
aoqi@0 414
aoqi@0 415
aoqi@0 416 void VMThread::loop() {
aoqi@0 417 assert(_cur_vm_operation == NULL, "no current one should be executing");
aoqi@0 418
aoqi@0 419 while(true) {
aoqi@0 420 VM_Operation* safepoint_ops = NULL;
aoqi@0 421 //
aoqi@0 422 // Wait for VM operation
aoqi@0 423 //
aoqi@0 424 // use no_safepoint_check to get lock without attempting to "sneak"
aoqi@0 425 { MutexLockerEx mu_queue(VMOperationQueue_lock,
aoqi@0 426 Mutex::_no_safepoint_check_flag);
aoqi@0 427
aoqi@0 428 // Look for new operation
aoqi@0 429 assert(_cur_vm_operation == NULL, "no current one should be executing");
aoqi@0 430 _cur_vm_operation = _vm_queue->remove_next();
aoqi@0 431
aoqi@0 432 // Stall time tracking code
aoqi@0 433 if (PrintVMQWaitTime && _cur_vm_operation != NULL &&
aoqi@0 434 !_cur_vm_operation->evaluate_concurrently()) {
aoqi@0 435 long stall = os::javaTimeMillis() - _cur_vm_operation->timestamp();
aoqi@0 436 if (stall > 0)
aoqi@0 437 tty->print_cr("%s stall: %Ld", _cur_vm_operation->name(), stall);
aoqi@0 438 }
aoqi@0 439
aoqi@0 440 while (!should_terminate() && _cur_vm_operation == NULL) {
aoqi@0 441 // wait with a timeout to guarantee safepoints at regular intervals
aoqi@0 442 bool timedout =
aoqi@0 443 VMOperationQueue_lock->wait(Mutex::_no_safepoint_check_flag,
aoqi@0 444 GuaranteedSafepointInterval);
aoqi@0 445
aoqi@0 446 // Support for self destruction
aoqi@0 447 if ((SelfDestructTimer != 0) && !is_error_reported() &&
aoqi@0 448 (os::elapsedTime() > SelfDestructTimer * 60)) {
aoqi@0 449 tty->print_cr("VM self-destructed");
aoqi@0 450 exit(-1);
aoqi@0 451 }
aoqi@0 452
aoqi@0 453 if (timedout && (SafepointALot ||
aoqi@0 454 SafepointSynchronize::is_cleanup_needed())) {
aoqi@0 455 MutexUnlockerEx mul(VMOperationQueue_lock,
aoqi@0 456 Mutex::_no_safepoint_check_flag);
aoqi@0 457 // Force a safepoint since we have not had one for at least
aoqi@0 458 // 'GuaranteedSafepointInterval' milliseconds. This will run all
aoqi@0 459 // the clean-up processing that needs to be done regularly at a
aoqi@0 460 // safepoint
aoqi@0 461 SafepointSynchronize::begin();
aoqi@0 462 #ifdef ASSERT
aoqi@0 463 if (GCALotAtAllSafepoints) InterfaceSupport::check_gc_alot();
aoqi@0 464 #endif
aoqi@0 465 SafepointSynchronize::end();
aoqi@0 466 }
aoqi@0 467 _cur_vm_operation = _vm_queue->remove_next();
aoqi@0 468
aoqi@0 469 // If we are at a safepoint we will evaluate all the operations that
aoqi@0 470 // follow that also require a safepoint
aoqi@0 471 if (_cur_vm_operation != NULL &&
aoqi@0 472 _cur_vm_operation->evaluate_at_safepoint()) {
aoqi@0 473 safepoint_ops = _vm_queue->drain_at_safepoint_priority();
aoqi@0 474 }
aoqi@0 475 }
aoqi@0 476
aoqi@0 477 if (should_terminate()) break;
aoqi@0 478 } // Release mu_queue_lock
aoqi@0 479
aoqi@0 480 //
aoqi@0 481 // Execute VM operation
aoqi@0 482 //
aoqi@0 483 { HandleMark hm(VMThread::vm_thread());
aoqi@0 484
aoqi@0 485 EventMark em("Executing VM operation: %s", vm_operation()->name());
aoqi@0 486 assert(_cur_vm_operation != NULL, "we should have found an operation to execute");
aoqi@0 487
aoqi@0 488 // Give the VM thread an extra quantum. Jobs tend to be bursty and this
aoqi@0 489 // helps the VM thread to finish up the job.
aoqi@0 490 // FIXME: When this is enabled and there are many threads, this can degrade
aoqi@0 491 // performance significantly.
aoqi@0 492 if( VMThreadHintNoPreempt )
aoqi@0 493 os::hint_no_preempt();
aoqi@0 494
aoqi@0 495 // If we are at a safepoint we will evaluate all the operations that
aoqi@0 496 // follow that also require a safepoint
aoqi@0 497 if (_cur_vm_operation->evaluate_at_safepoint()) {
aoqi@0 498
aoqi@0 499 _vm_queue->set_drain_list(safepoint_ops); // ensure ops can be scanned
aoqi@0 500
aoqi@0 501 SafepointSynchronize::begin();
aoqi@0 502 evaluate_operation(_cur_vm_operation);
aoqi@0 503 // now process all queued safepoint ops, iteratively draining
aoqi@0 504 // the queue until there are none left
aoqi@0 505 do {
aoqi@0 506 _cur_vm_operation = safepoint_ops;
aoqi@0 507 if (_cur_vm_operation != NULL) {
aoqi@0 508 do {
aoqi@0 509 // evaluate_operation deletes the op object so we have
aoqi@0 510 // to grab the next op now
aoqi@0 511 VM_Operation* next = _cur_vm_operation->next();
aoqi@0 512 _vm_queue->set_drain_list(next);
aoqi@0 513 evaluate_operation(_cur_vm_operation);
aoqi@0 514 _cur_vm_operation = next;
aoqi@0 515 if (PrintSafepointStatistics) {
aoqi@0 516 SafepointSynchronize::inc_vmop_coalesced_count();
aoqi@0 517 }
aoqi@0 518 } while (_cur_vm_operation != NULL);
aoqi@0 519 }
aoqi@0 520 // There is a chance that a thread enqueued a safepoint op
aoqi@0 521 // since we released the op-queue lock and initiated the safepoint.
aoqi@0 522 // So we drain the queue again if there is anything there, as an
aoqi@0 523 // optimization to try and reduce the number of safepoints.
aoqi@0 524 // As the safepoint synchronizes us with JavaThreads we will see
aoqi@0 525 // any enqueue made by a JavaThread, but the peek will not
aoqi@0 526 // necessarily detect a concurrent enqueue by a GC thread, but
aoqi@0 527 // that simply means the op will wait for the next major cycle of the
aoqi@0 528 // VMThread - just as it would if the GC thread lost the race for
aoqi@0 529 // the lock.
aoqi@0 530 if (_vm_queue->peek_at_safepoint_priority()) {
aoqi@0 531 // must hold lock while draining queue
aoqi@0 532 MutexLockerEx mu_queue(VMOperationQueue_lock,
aoqi@0 533 Mutex::_no_safepoint_check_flag);
aoqi@0 534 safepoint_ops = _vm_queue->drain_at_safepoint_priority();
aoqi@0 535 } else {
aoqi@0 536 safepoint_ops = NULL;
aoqi@0 537 }
aoqi@0 538 } while(safepoint_ops != NULL);
aoqi@0 539
aoqi@0 540 _vm_queue->set_drain_list(NULL);
aoqi@0 541
aoqi@0 542 // Complete safepoint synchronization
aoqi@0 543 SafepointSynchronize::end();
aoqi@0 544
aoqi@0 545 } else { // not a safepoint operation
aoqi@0 546 if (TraceLongCompiles) {
aoqi@0 547 elapsedTimer t;
aoqi@0 548 t.start();
aoqi@0 549 evaluate_operation(_cur_vm_operation);
aoqi@0 550 t.stop();
aoqi@0 551 double secs = t.seconds();
aoqi@0 552 if (secs * 1e3 > LongCompileThreshold) {
aoqi@0 553 // XXX - _cur_vm_operation should not be accessed after
aoqi@0 554 // the completed count has been incremented; the waiting
aoqi@0 555 // thread may have already freed this memory.
aoqi@0 556 tty->print_cr("vm %s: %3.7f secs]", _cur_vm_operation->name(), secs);
aoqi@0 557 }
aoqi@0 558 } else {
aoqi@0 559 evaluate_operation(_cur_vm_operation);
aoqi@0 560 }
aoqi@0 561
aoqi@0 562 _cur_vm_operation = NULL;
aoqi@0 563 }
aoqi@0 564 }
aoqi@0 565
aoqi@0 566 //
aoqi@0 567 // Notify (potential) waiting Java thread(s) - lock without safepoint
aoqi@0 568 // check so that sneaking is not possible
aoqi@0 569 { MutexLockerEx mu(VMOperationRequest_lock,
aoqi@0 570 Mutex::_no_safepoint_check_flag);
aoqi@0 571 VMOperationRequest_lock->notify_all();
aoqi@0 572 }
aoqi@0 573
aoqi@0 574 //
aoqi@0 575 // We want to make sure that we get to a safepoint regularly.
aoqi@0 576 //
aoqi@0 577 if (SafepointALot || SafepointSynchronize::is_cleanup_needed()) {
aoqi@0 578 long interval = SafepointSynchronize::last_non_safepoint_interval();
aoqi@0 579 bool max_time_exceeded = GuaranteedSafepointInterval != 0 && (interval > GuaranteedSafepointInterval);
aoqi@0 580 if (SafepointALot || max_time_exceeded) {
aoqi@0 581 HandleMark hm(VMThread::vm_thread());
aoqi@0 582 SafepointSynchronize::begin();
aoqi@0 583 SafepointSynchronize::end();
aoqi@0 584 }
aoqi@0 585 }
aoqi@0 586 }
aoqi@0 587 }
aoqi@0 588
aoqi@0 589 void VMThread::execute(VM_Operation* op) {
aoqi@0 590 Thread* t = Thread::current();
aoqi@0 591
aoqi@0 592 if (!t->is_VM_thread()) {
aoqi@0 593 SkipGCALot sgcalot(t); // avoid re-entrant attempts to gc-a-lot
aoqi@0 594 // JavaThread or WatcherThread
aoqi@0 595 bool concurrent = op->evaluate_concurrently();
aoqi@0 596 // only blocking VM operations need to verify the caller's safepoint state:
aoqi@0 597 if (!concurrent) {
aoqi@0 598 t->check_for_valid_safepoint_state(true);
aoqi@0 599 }
aoqi@0 600
aoqi@0 601 // New request from Java thread, evaluate prologue
aoqi@0 602 if (!op->doit_prologue()) {
aoqi@0 603 return; // op was cancelled
aoqi@0 604 }
aoqi@0 605
aoqi@0 606 // Setup VM_operations for execution
aoqi@0 607 op->set_calling_thread(t, Thread::get_priority(t));
aoqi@0 608
aoqi@0 609 // It does not make sense to execute the epilogue, if the VM operation object is getting
aoqi@0 610 // deallocated by the VM thread.
aoqi@0 611 bool execute_epilog = !op->is_cheap_allocated();
aoqi@0 612 assert(!concurrent || op->is_cheap_allocated(), "concurrent => cheap_allocated");
aoqi@0 613
aoqi@0 614 // Get ticket number for non-concurrent VM operations
aoqi@0 615 int ticket = 0;
aoqi@0 616 if (!concurrent) {
aoqi@0 617 ticket = t->vm_operation_ticket();
aoqi@0 618 }
aoqi@0 619
aoqi@0 620 // Add VM operation to list of waiting threads. We are guaranteed not to block while holding the
aoqi@0 621 // VMOperationQueue_lock, so we can block without a safepoint check. This allows vm operation requests
aoqi@0 622 // to be queued up during a safepoint synchronization.
aoqi@0 623 {
aoqi@0 624 VMOperationQueue_lock->lock_without_safepoint_check();
aoqi@0 625 bool ok = _vm_queue->add(op);
aoqi@0 626 op->set_timestamp(os::javaTimeMillis());
aoqi@0 627 VMOperationQueue_lock->notify();
aoqi@0 628 VMOperationQueue_lock->unlock();
aoqi@0 629 // VM_Operation got skipped
aoqi@0 630 if (!ok) {
aoqi@0 631 assert(concurrent, "can only skip concurrent tasks");
aoqi@0 632 if (op->is_cheap_allocated()) delete op;
aoqi@0 633 return;
aoqi@0 634 }
aoqi@0 635 }
aoqi@0 636
aoqi@0 637 if (!concurrent) {
aoqi@0 638 // Wait for completion of request (non-concurrent)
aoqi@0 639 // Note: only a JavaThread triggers the safepoint check when locking
aoqi@0 640 MutexLocker mu(VMOperationRequest_lock);
aoqi@0 641 while(t->vm_operation_completed_count() < ticket) {
aoqi@0 642 VMOperationRequest_lock->wait(!t->is_Java_thread());
aoqi@0 643 }
aoqi@0 644 }
aoqi@0 645
aoqi@0 646 if (execute_epilog) {
aoqi@0 647 op->doit_epilogue();
aoqi@0 648 }
aoqi@0 649 } else {
aoqi@0 650 // invoked by VM thread; usually nested VM operation
aoqi@0 651 assert(t->is_VM_thread(), "must be a VM thread");
aoqi@0 652 VM_Operation* prev_vm_operation = vm_operation();
aoqi@0 653 if (prev_vm_operation != NULL) {
aoqi@0 654 // Check the VM operation allows nested VM operation. This normally not the case, e.g., the compiler
aoqi@0 655 // does not allow nested scavenges or compiles.
aoqi@0 656 if (!prev_vm_operation->allow_nested_vm_operations()) {
aoqi@0 657 fatal(err_msg("Nested VM operation %s requested by operation %s",
aoqi@0 658 op->name(), vm_operation()->name()));
aoqi@0 659 }
aoqi@0 660 op->set_calling_thread(prev_vm_operation->calling_thread(), prev_vm_operation->priority());
aoqi@0 661 }
aoqi@0 662
aoqi@0 663 EventMark em("Executing %s VM operation: %s", prev_vm_operation ? "nested" : "", op->name());
aoqi@0 664
aoqi@0 665 // Release all internal handles after operation is evaluated
aoqi@0 666 HandleMark hm(t);
aoqi@0 667 _cur_vm_operation = op;
aoqi@0 668
aoqi@0 669 if (op->evaluate_at_safepoint() && !SafepointSynchronize::is_at_safepoint()) {
aoqi@0 670 SafepointSynchronize::begin();
aoqi@0 671 op->evaluate();
aoqi@0 672 SafepointSynchronize::end();
aoqi@0 673 } else {
aoqi@0 674 op->evaluate();
aoqi@0 675 }
aoqi@0 676
aoqi@0 677 // Free memory if needed
aoqi@0 678 if (op->is_cheap_allocated()) delete op;
aoqi@0 679
aoqi@0 680 _cur_vm_operation = prev_vm_operation;
aoqi@0 681 }
aoqi@0 682 }
aoqi@0 683
aoqi@0 684
aoqi@0 685 void VMThread::oops_do(OopClosure* f, CLDToOopClosure* cld_f, CodeBlobClosure* cf) {
aoqi@0 686 Thread::oops_do(f, cld_f, cf);
aoqi@0 687 _vm_queue->oops_do(f);
aoqi@0 688 }
aoqi@0 689
aoqi@0 690 //------------------------------------------------------------------------------------------------------------------
aoqi@0 691 #ifndef PRODUCT
aoqi@0 692
aoqi@0 693 void VMOperationQueue::verify_queue(int prio) {
aoqi@0 694 // Check that list is correctly linked
aoqi@0 695 int length = _queue_length[prio];
aoqi@0 696 VM_Operation *cur = _queue[prio];
aoqi@0 697 int i;
aoqi@0 698
aoqi@0 699 // Check forward links
aoqi@0 700 for(i = 0; i < length; i++) {
aoqi@0 701 cur = cur->next();
aoqi@0 702 assert(cur != _queue[prio], "list to short (forward)");
aoqi@0 703 }
aoqi@0 704 assert(cur->next() == _queue[prio], "list to long (forward)");
aoqi@0 705
aoqi@0 706 // Check backwards links
aoqi@0 707 cur = _queue[prio];
aoqi@0 708 for(i = 0; i < length; i++) {
aoqi@0 709 cur = cur->prev();
aoqi@0 710 assert(cur != _queue[prio], "list to short (backwards)");
aoqi@0 711 }
aoqi@0 712 assert(cur->prev() == _queue[prio], "list to long (backwards)");
aoqi@0 713 }
aoqi@0 714
aoqi@0 715 #endif
aoqi@0 716
aoqi@0 717 void VMThread::verify() {
aoqi@0 718 oops_do(&VerifyOopClosure::verify_oop, NULL, NULL);
aoqi@0 719 }

mercurial