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