1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/gc_implementation/parallelScavenge/gcTaskThread.cpp Wed Apr 27 01:25:04 2016 +0800 1.3 @@ -0,0 +1,180 @@ 1.4 + 1.5 +/* 1.6 + * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved. 1.7 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.8 + * 1.9 + * This code is free software; you can redistribute it and/or modify it 1.10 + * under the terms of the GNU General Public License version 2 only, as 1.11 + * published by the Free Software Foundation. 1.12 + * 1.13 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.14 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.15 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.16 + * version 2 for more details (a copy is included in the LICENSE file that 1.17 + * accompanied this code). 1.18 + * 1.19 + * You should have received a copy of the GNU General Public License version 1.20 + * 2 along with this work; if not, write to the Free Software Foundation, 1.21 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.22 + * 1.23 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.24 + * or visit www.oracle.com if you need additional information or have any 1.25 + * questions. 1.26 + * 1.27 + */ 1.28 + 1.29 +#include "precompiled.hpp" 1.30 +#include "gc_implementation/parallelScavenge/gcTaskManager.hpp" 1.31 +#include "gc_implementation/parallelScavenge/gcTaskThread.hpp" 1.32 +#include "memory/allocation.hpp" 1.33 +#include "memory/allocation.inline.hpp" 1.34 +#include "memory/resourceArea.hpp" 1.35 +#include "runtime/handles.hpp" 1.36 +#include "runtime/handles.inline.hpp" 1.37 +#include "runtime/os.hpp" 1.38 +#include "runtime/thread.hpp" 1.39 + 1.40 +PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC 1.41 + 1.42 +GCTaskThread::GCTaskThread(GCTaskManager* manager, 1.43 + uint which, 1.44 + uint processor_id) : 1.45 + _manager(manager), 1.46 + _processor_id(processor_id), 1.47 + _time_stamps(NULL), 1.48 + _time_stamp_index(0) 1.49 +{ 1.50 + if (!os::create_thread(this, os::pgc_thread)) 1.51 + vm_exit_out_of_memory(0, OOM_MALLOC_ERROR, "Cannot create GC thread. Out of system resources."); 1.52 + 1.53 + if (PrintGCTaskTimeStamps) { 1.54 + _time_stamps = NEW_C_HEAP_ARRAY(GCTaskTimeStamp, GCTaskTimeStampEntries, mtGC); 1.55 + 1.56 + guarantee(_time_stamps != NULL, "Sanity"); 1.57 + } 1.58 + set_id(which); 1.59 + set_name("GC task thread#%d (ParallelGC)", which); 1.60 +} 1.61 + 1.62 +GCTaskThread::~GCTaskThread() { 1.63 + if (_time_stamps != NULL) { 1.64 + FREE_C_HEAP_ARRAY(GCTaskTimeStamp, _time_stamps, mtGC); 1.65 + } 1.66 +} 1.67 + 1.68 +void GCTaskThread::start() { 1.69 + os::start_thread(this); 1.70 +} 1.71 + 1.72 +GCTaskTimeStamp* GCTaskThread::time_stamp_at(uint index) { 1.73 + guarantee(index < GCTaskTimeStampEntries, "increase GCTaskTimeStampEntries"); 1.74 + 1.75 + return &(_time_stamps[index]); 1.76 +} 1.77 + 1.78 +void GCTaskThread::print_task_time_stamps() { 1.79 + assert(PrintGCTaskTimeStamps, "Sanity"); 1.80 + assert(_time_stamps != NULL, "Sanity (Probably set PrintGCTaskTimeStamps late)"); 1.81 + 1.82 + tty->print_cr("GC-Thread %u entries: %d", id(), _time_stamp_index); 1.83 + for(uint i=0; i<_time_stamp_index; i++) { 1.84 + GCTaskTimeStamp* time_stamp = time_stamp_at(i); 1.85 + tty->print_cr("\t[ %s " INT64_FORMAT " " INT64_FORMAT " ]", 1.86 + time_stamp->name(), 1.87 + time_stamp->entry_time(), 1.88 + time_stamp->exit_time()); 1.89 + } 1.90 + 1.91 + // Reset after dumping the data 1.92 + _time_stamp_index = 0; 1.93 +} 1.94 + 1.95 +void GCTaskThread::print_on(outputStream* st) const { 1.96 + st->print("\"%s\" ", name()); 1.97 + Thread::print_on(st); 1.98 + st->cr(); 1.99 +} 1.100 + 1.101 +// GC workers get tasks from the GCTaskManager and execute 1.102 +// them in this method. If there are no tasks to execute, 1.103 +// the GC workers wait in the GCTaskManager's get_task() 1.104 +// for tasks to be enqueued for execution. 1.105 + 1.106 +void GCTaskThread::run() { 1.107 + // Set up the thread for stack overflow support 1.108 + this->record_stack_base_and_size(); 1.109 + this->initialize_thread_local_storage(); 1.110 + // Bind yourself to your processor. 1.111 + if (processor_id() != GCTaskManager::sentinel_worker()) { 1.112 + if (TraceGCTaskThread) { 1.113 + tty->print_cr("GCTaskThread::run: " 1.114 + " binding to processor %u", processor_id()); 1.115 + } 1.116 + if (!os::bind_to_processor(processor_id())) { 1.117 + DEBUG_ONLY( 1.118 + warning("Couldn't bind GCTaskThread %u to processor %u", 1.119 + which(), processor_id()); 1.120 + ) 1.121 + } 1.122 + } 1.123 + // Part of thread setup. 1.124 + // ??? Are these set up once here to make subsequent ones fast? 1.125 + HandleMark hm_outer; 1.126 + ResourceMark rm_outer; 1.127 + 1.128 + TimeStamp timer; 1.129 + 1.130 + for (;/* ever */;) { 1.131 + // These are so we can flush the resources allocated in the inner loop. 1.132 + HandleMark hm_inner; 1.133 + ResourceMark rm_inner; 1.134 + for (; /* break */; ) { 1.135 + // This will block until there is a task to be gotten. 1.136 + GCTask* task = manager()->get_task(which()); 1.137 + // Record if this is an idle task for later use. 1.138 + bool is_idle_task = task->is_idle_task(); 1.139 + // In case the update is costly 1.140 + if (PrintGCTaskTimeStamps) { 1.141 + timer.update(); 1.142 + } 1.143 + 1.144 + jlong entry_time = timer.ticks(); 1.145 + char* name = task->name(); 1.146 + 1.147 + // If this is the barrier task, it can be destroyed 1.148 + // by the GC task manager once the do_it() executes. 1.149 + task->do_it(manager(), which()); 1.150 + 1.151 + // Use the saved value of is_idle_task because references 1.152 + // using "task" are not reliable for the barrier task. 1.153 + if (!is_idle_task) { 1.154 + manager()->note_completion(which()); 1.155 + 1.156 + if (PrintGCTaskTimeStamps) { 1.157 + assert(_time_stamps != NULL, 1.158 + "Sanity (PrintGCTaskTimeStamps set late?)"); 1.159 + 1.160 + timer.update(); 1.161 + 1.162 + GCTaskTimeStamp* time_stamp = time_stamp_at(_time_stamp_index++); 1.163 + 1.164 + time_stamp->set_name(name); 1.165 + time_stamp->set_entry_time(entry_time); 1.166 + time_stamp->set_exit_time(timer.ticks()); 1.167 + } 1.168 + } else { 1.169 + // idle tasks complete outside the normal accounting 1.170 + // so that a task can complete without waiting for idle tasks. 1.171 + // They have to be terminated separately. 1.172 + IdleGCTask::destroy((IdleGCTask*)task); 1.173 + set_is_working(true); 1.174 + } 1.175 + 1.176 + // Check if we should release our inner resources. 1.177 + if (manager()->should_release_resources(which())) { 1.178 + manager()->note_release(which()); 1.179 + break; 1.180 + } 1.181 + } 1.182 + } 1.183 +}