src/share/vm/runtime/vmThread.cpp

Wed, 06 Jul 2011 13:02:54 -0700

author
jcoomes
date
Wed, 06 Jul 2011 13:02:54 -0700
changeset 2997
bf6481e5f96d
parent 2825
1f4413413144
child 3156
f08d439fab8c
permissions
-rw-r--r--

7061225: os::print_cpu_info() should support os-specific data
Reviewed-by: dholmes, never, jwilhelm, kvn

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

mercurial