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 "gc/parallel/gcTaskManager.hpp"
27 #include "gc/parallel/gcTaskThread.hpp"
28 #include "gc/shared/adaptiveSizePolicy.hpp"
29 #include "gc/shared/gcId.hpp"
30 #include "memory/allocation.hpp"
31 #include "memory/allocation.inline.hpp"
32 #include "runtime/mutex.hpp"
33 #include "runtime/mutexLocker.hpp"
34 #include "runtime/orderAccess.inline.hpp"
35
36 //
37 // GCTask
38 //
39
40 const char* GCTask::Kind::to_string(kind value) {
41 const char* result = "unknown GCTask kind";
42 switch (value) {
43 default:
44 result = "unknown GCTask kind";
45 break;
46 case unknown_task:
47 result = "unknown task";
48 break;
49 case ordinary_task:
448 GCTaskQueue::destroy(unsynchronized_queue);
449 SynchronizedGCTaskQueue::destroy(queue());
450 _queue = NULL;
451 }
452 if (monitor() != NULL) {
453 delete monitor();
454 _monitor = NULL;
455 }
456 }
457
458 void GCTaskManager::set_active_gang() {
459 _active_workers =
460 AdaptiveSizePolicy::calc_active_workers(workers(),
461 active_workers(),
462 Threads::number_of_non_daemon_threads());
463
464 assert(!all_workers_active() || active_workers() == ParallelGCThreads,
465 "all_workers_active() is incorrect: "
466 "active %d ParallelGCThreads %u", active_workers(),
467 ParallelGCThreads);
468 if (TraceDynamicGCThreads) {
469 gclog_or_tty->print_cr("GCTaskManager::set_active_gang(): "
470 "all_workers_active() %d workers %d "
471 "active %d ParallelGCThreads %u",
472 all_workers_active(), workers(), active_workers(),
473 ParallelGCThreads);
474 }
475 }
476
477 // Create IdleGCTasks for inactive workers.
478 // Creates tasks in a ResourceArea and assumes
479 // an appropriate ResourceMark.
480 void GCTaskManager::task_idle_workers() {
481 {
482 int more_inactive_workers = 0;
483 {
484 // Stop any idle tasks from exiting their IdleGCTask's
485 // and get the count for additional IdleGCTask's under
486 // the GCTaskManager's monitor so that the "more_inactive_workers"
487 // count is correct.
488 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
489 _wait_helper.set_should_wait(true);
490 // active_workers are a number being requested. idle_workers
491 // are the number currently idle. If all the workers are being
492 // requested to be active but some are already idle, reduce
493 // the number of active_workers to be consistent with the
494 // number of idle_workers. The idle_workers are stuck in
495 // idle tasks and will no longer be release (since a new GC
496 // is starting). Try later to release enough idle_workers
497 // to allow the desired number of active_workers.
498 more_inactive_workers =
499 workers() - active_workers() - idle_workers();
500 if (more_inactive_workers < 0) {
501 int reduced_active_workers = active_workers() + more_inactive_workers;
502 set_active_workers(reduced_active_workers);
503 more_inactive_workers = 0;
504 }
505 if (TraceDynamicGCThreads) {
506 gclog_or_tty->print_cr("JT: %d workers %d active %d "
507 "idle %d more %d",
508 Threads::number_of_non_daemon_threads(),
509 workers(),
510 active_workers(),
511 idle_workers(),
512 more_inactive_workers);
513 }
514 }
515 GCTaskQueue* q = GCTaskQueue::create();
516 for(uint i = 0; i < (uint) more_inactive_workers; i++) {
517 q->enqueue(IdleGCTask::create_on_c_heap());
518 increment_idle_workers();
519 }
520 assert(workers() == active_workers() + idle_workers(),
521 "total workers should equal active + inactive");
522 add_list(q);
523 // GCTaskQueue* q was created in a ResourceArea so a
524 // destroy() call is not needed.
525 }
526 }
527
528 void GCTaskManager::release_idle_workers() {
529 {
530 MutexLockerEx ml(monitor(),
531 Mutex::_no_safepoint_check_flag);
532 _wait_helper.set_should_wait(false);
533 monitor()->notify_all();
534 // Release monitor
535 }
536 }
537
538 void GCTaskManager::print_task_time_stamps() {
539 for(uint i=0; i<ParallelGCThreads; i++) {
540 GCTaskThread* t = thread(i);
541 t->print_task_time_stamps();
542 }
543 }
544
545 void GCTaskManager::print_threads_on(outputStream* st) {
546 uint num_thr = workers();
547 for (uint i = 0; i < num_thr; i++) {
548 thread(i)->print_on(st);
549 st->cr();
550 }
551 }
552
553 void GCTaskManager::threads_do(ThreadClosure* tc) {
554 assert(tc != NULL, "Null ThreadClosure");
555 uint num_thr = workers();
556 for (uint i = 0; i < num_thr; i++) {
557 tc->do_thread(thread(i));
558 }
811 //
812 // IdleGCTask
813 //
814
815 IdleGCTask* IdleGCTask::create() {
816 IdleGCTask* result = new IdleGCTask(false);
817 assert(UseDynamicNumberOfGCThreads,
818 "Should only be used with dynamic GC thread");
819 return result;
820 }
821
822 IdleGCTask* IdleGCTask::create_on_c_heap() {
823 IdleGCTask* result = new(ResourceObj::C_HEAP, mtGC) IdleGCTask(true);
824 assert(UseDynamicNumberOfGCThreads,
825 "Should only be used with dynamic GC thread");
826 return result;
827 }
828
829 void IdleGCTask::do_it(GCTaskManager* manager, uint which) {
830 WaitHelper* wait_helper = manager->wait_helper();
831 if (TraceGCTaskManager) {
832 tty->print_cr("[" INTPTR_FORMAT "]"
833 " IdleGCTask:::do_it()"
834 " should_wait: %s",
835 p2i(this), wait_helper->should_wait() ? "true" : "false");
836 }
837 MutexLockerEx ml(manager->monitor(), Mutex::_no_safepoint_check_flag);
838 if (TraceDynamicGCThreads) {
839 gclog_or_tty->print_cr("--- idle %d", which);
840 }
841 // Increment has to be done when the idle tasks are created.
842 // manager->increment_idle_workers();
843 manager->monitor()->notify_all();
844 while (wait_helper->should_wait()) {
845 if (TraceGCTaskManager) {
846 tty->print_cr("[" INTPTR_FORMAT "]"
847 " IdleGCTask::do_it()"
848 " [" INTPTR_FORMAT "] (%s)->wait()",
849 p2i(this), p2i(manager->monitor()), manager->monitor()->name());
850 }
851 manager->monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
852 }
853 manager->decrement_idle_workers();
854 if (TraceDynamicGCThreads) {
855 gclog_or_tty->print_cr("--- release %d", which);
856 }
857 if (TraceGCTaskManager) {
858 tty->print_cr("[" INTPTR_FORMAT "]"
859 " IdleGCTask::do_it() returns"
860 " should_wait: %s",
861 p2i(this), wait_helper->should_wait() ? "true" : "false");
862 }
863 // Release monitor().
864 }
865
866 void IdleGCTask::destroy(IdleGCTask* that) {
867 if (that != NULL) {
868 that->destruct();
869 if (that->is_c_heap_obj()) {
870 FreeHeap(that);
871 }
872 }
873 }
874
875 void IdleGCTask::destruct() {
876 // This has to know it's superclass structure, just like the constructor.
877 this->GCTask::destruct();
878 // Nothing else to do.
879 }
880
881 //
882 // WaitForBarrierGCTask
|
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 "gc/parallel/gcTaskManager.hpp"
27 #include "gc/parallel/gcTaskThread.hpp"
28 #include "gc/shared/adaptiveSizePolicy.hpp"
29 #include "gc/shared/gcId.hpp"
30 #include "logging/log.hpp"
31 #include "memory/allocation.hpp"
32 #include "memory/allocation.inline.hpp"
33 #include "runtime/mutex.hpp"
34 #include "runtime/mutexLocker.hpp"
35 #include "runtime/orderAccess.inline.hpp"
36
37 //
38 // GCTask
39 //
40
41 const char* GCTask::Kind::to_string(kind value) {
42 const char* result = "unknown GCTask kind";
43 switch (value) {
44 default:
45 result = "unknown GCTask kind";
46 break;
47 case unknown_task:
48 result = "unknown task";
49 break;
50 case ordinary_task:
449 GCTaskQueue::destroy(unsynchronized_queue);
450 SynchronizedGCTaskQueue::destroy(queue());
451 _queue = NULL;
452 }
453 if (monitor() != NULL) {
454 delete monitor();
455 _monitor = NULL;
456 }
457 }
458
459 void GCTaskManager::set_active_gang() {
460 _active_workers =
461 AdaptiveSizePolicy::calc_active_workers(workers(),
462 active_workers(),
463 Threads::number_of_non_daemon_threads());
464
465 assert(!all_workers_active() || active_workers() == ParallelGCThreads,
466 "all_workers_active() is incorrect: "
467 "active %d ParallelGCThreads %u", active_workers(),
468 ParallelGCThreads);
469 log_trace(gc, task)("GCTaskManager::set_active_gang(): "
470 "all_workers_active() %d workers %d "
471 "active %d ParallelGCThreads %u",
472 all_workers_active(), workers(), active_workers(),
473 ParallelGCThreads);
474 }
475
476 // Create IdleGCTasks for inactive workers.
477 // Creates tasks in a ResourceArea and assumes
478 // an appropriate ResourceMark.
479 void GCTaskManager::task_idle_workers() {
480 {
481 int more_inactive_workers = 0;
482 {
483 // Stop any idle tasks from exiting their IdleGCTask's
484 // and get the count for additional IdleGCTask's under
485 // the GCTaskManager's monitor so that the "more_inactive_workers"
486 // count is correct.
487 MutexLockerEx ml(monitor(), Mutex::_no_safepoint_check_flag);
488 _wait_helper.set_should_wait(true);
489 // active_workers are a number being requested. idle_workers
490 // are the number currently idle. If all the workers are being
491 // requested to be active but some are already idle, reduce
492 // the number of active_workers to be consistent with the
493 // number of idle_workers. The idle_workers are stuck in
494 // idle tasks and will no longer be release (since a new GC
495 // is starting). Try later to release enough idle_workers
496 // to allow the desired number of active_workers.
497 more_inactive_workers =
498 workers() - active_workers() - idle_workers();
499 if (more_inactive_workers < 0) {
500 int reduced_active_workers = active_workers() + more_inactive_workers;
501 set_active_workers(reduced_active_workers);
502 more_inactive_workers = 0;
503 }
504 log_trace(gc, task)("JT: %d workers %d active %d idle %d more %d",
505 Threads::number_of_non_daemon_threads(),
506 workers(),
507 active_workers(),
508 idle_workers(),
509 more_inactive_workers);
510 }
511 GCTaskQueue* q = GCTaskQueue::create();
512 for(uint i = 0; i < (uint) more_inactive_workers; i++) {
513 q->enqueue(IdleGCTask::create_on_c_heap());
514 increment_idle_workers();
515 }
516 assert(workers() == active_workers() + idle_workers(),
517 "total workers should equal active + inactive");
518 add_list(q);
519 // GCTaskQueue* q was created in a ResourceArea so a
520 // destroy() call is not needed.
521 }
522 }
523
524 void GCTaskManager::release_idle_workers() {
525 {
526 MutexLockerEx ml(monitor(),
527 Mutex::_no_safepoint_check_flag);
528 _wait_helper.set_should_wait(false);
529 monitor()->notify_all();
530 // Release monitor
531 }
532 }
533
534 void GCTaskManager::print_task_time_stamps() {
535 if (!log_is_enabled(Debug, gc, task, time)) {
536 return;
537 }
538 for(uint i=0; i<ParallelGCThreads; i++) {
539 GCTaskThread* t = thread(i);
540 t->print_task_time_stamps();
541 }
542 }
543
544 void GCTaskManager::print_threads_on(outputStream* st) {
545 uint num_thr = workers();
546 for (uint i = 0; i < num_thr; i++) {
547 thread(i)->print_on(st);
548 st->cr();
549 }
550 }
551
552 void GCTaskManager::threads_do(ThreadClosure* tc) {
553 assert(tc != NULL, "Null ThreadClosure");
554 uint num_thr = workers();
555 for (uint i = 0; i < num_thr; i++) {
556 tc->do_thread(thread(i));
557 }
810 //
811 // IdleGCTask
812 //
813
814 IdleGCTask* IdleGCTask::create() {
815 IdleGCTask* result = new IdleGCTask(false);
816 assert(UseDynamicNumberOfGCThreads,
817 "Should only be used with dynamic GC thread");
818 return result;
819 }
820
821 IdleGCTask* IdleGCTask::create_on_c_heap() {
822 IdleGCTask* result = new(ResourceObj::C_HEAP, mtGC) IdleGCTask(true);
823 assert(UseDynamicNumberOfGCThreads,
824 "Should only be used with dynamic GC thread");
825 return result;
826 }
827
828 void IdleGCTask::do_it(GCTaskManager* manager, uint which) {
829 WaitHelper* wait_helper = manager->wait_helper();
830 log_trace(gc, task)("[" INTPTR_FORMAT "] IdleGCTask:::do_it() should_wait: %s",
831 p2i(this), wait_helper->should_wait() ? "true" : "false");
832
833 MutexLockerEx ml(manager->monitor(), Mutex::_no_safepoint_check_flag);
834 log_trace(gc, task)("--- idle %d", which);
835 // Increment has to be done when the idle tasks are created.
836 // manager->increment_idle_workers();
837 manager->monitor()->notify_all();
838 while (wait_helper->should_wait()) {
839 log_trace(gc, task)("[" INTPTR_FORMAT "] IdleGCTask::do_it() [" INTPTR_FORMAT "] (%s)->wait()",
840 p2i(this), p2i(manager->monitor()), manager->monitor()->name());
841 manager->monitor()->wait(Mutex::_no_safepoint_check_flag, 0);
842 }
843 manager->decrement_idle_workers();
844
845 log_trace(gc, task)("--- release %d", which);
846 log_trace(gc, task)("[" INTPTR_FORMAT "] IdleGCTask::do_it() returns should_wait: %s",
847 p2i(this), wait_helper->should_wait() ? "true" : "false");
848 // Release monitor().
849 }
850
851 void IdleGCTask::destroy(IdleGCTask* that) {
852 if (that != NULL) {
853 that->destruct();
854 if (that->is_c_heap_obj()) {
855 FreeHeap(that);
856 }
857 }
858 }
859
860 void IdleGCTask::destruct() {
861 // This has to know it's superclass structure, just like the constructor.
862 this->GCTask::destruct();
863 // Nothing else to do.
864 }
865
866 //
867 // WaitForBarrierGCTask
|