10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "gc/parallel/gcTaskManager.hpp" 28 #include "gc/parallel/gcTaskThread.hpp" 29 #include "gc/shared/gcId.hpp" 30 #include "memory/allocation.hpp" 31 #include "memory/allocation.inline.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "runtime/handles.hpp" 34 #include "runtime/handles.inline.hpp" 35 #include "runtime/os.hpp" 36 #include "runtime/thread.hpp" 37 38 GCTaskThread::GCTaskThread(GCTaskManager* manager, 39 uint which, 40 uint processor_id) : 41 _manager(manager), 42 _processor_id(processor_id), 43 _time_stamps(NULL), 44 _time_stamp_index(0) 45 { 46 if (!os::create_thread(this, os::pgc_thread)) 47 vm_exit_out_of_memory(0, OOM_MALLOC_ERROR, "Cannot create GC thread. Out of system resources."); 48 49 if (PrintGCTaskTimeStamps) { 50 _time_stamps = NEW_C_HEAP_ARRAY(GCTaskTimeStamp, GCTaskTimeStampEntries, mtGC); 51 52 guarantee(_time_stamps != NULL, "Sanity"); 53 } 54 set_id(which); 55 set_name("ParGC Thread#%d", which); 56 } 57 58 GCTaskThread::~GCTaskThread() { 59 if (_time_stamps != NULL) { 60 FREE_C_HEAP_ARRAY(GCTaskTimeStamp, _time_stamps); 61 } 62 } 63 64 void GCTaskThread::start() { 65 os::start_thread(this); 66 } 67 68 GCTaskTimeStamp* GCTaskThread::time_stamp_at(uint index) { 69 guarantee(index < GCTaskTimeStampEntries, "increase GCTaskTimeStampEntries"); 70 71 return &(_time_stamps[index]); 72 } 73 74 void GCTaskThread::print_task_time_stamps() { 75 assert(PrintGCTaskTimeStamps, "Sanity"); 76 assert(_time_stamps != NULL, "Sanity (Probably set PrintGCTaskTimeStamps late)"); 77 78 tty->print_cr("GC-Thread %u entries: %d", id(), _time_stamp_index); 79 for(uint i=0; i<_time_stamp_index; i++) { 80 GCTaskTimeStamp* time_stamp = time_stamp_at(i); 81 tty->print_cr("\t[ %s " JLONG_FORMAT " " JLONG_FORMAT " ]", 82 time_stamp->name(), 83 time_stamp->entry_time(), 84 time_stamp->exit_time()); 85 } 86 87 // Reset after dumping the data 88 _time_stamp_index = 0; 89 } 90 91 // GC workers get tasks from the GCTaskManager and execute 92 // them in this method. If there are no tasks to execute, 93 // the GC workers wait in the GCTaskManager's get_task() 94 // for tasks to be enqueued for execution. 95 96 void GCTaskThread::run() { 97 // Set up the thread for stack overflow support 98 this->record_stack_base_and_size(); 99 this->initialize_thread_local_storage(); 100 this->initialize_named_thread(); 101 // Bind yourself to your processor. 112 } 113 } 114 // Part of thread setup. 115 // ??? Are these set up once here to make subsequent ones fast? 116 HandleMark hm_outer; 117 ResourceMark rm_outer; 118 119 TimeStamp timer; 120 121 for (;/* ever */;) { 122 // These are so we can flush the resources allocated in the inner loop. 123 HandleMark hm_inner; 124 ResourceMark rm_inner; 125 for (; /* break */; ) { 126 // This will block until there is a task to be gotten. 127 GCTask* task = manager()->get_task(which()); 128 GCIdMark gc_id_mark(task->gc_id()); 129 // Record if this is an idle task for later use. 130 bool is_idle_task = task->is_idle_task(); 131 // In case the update is costly 132 if (PrintGCTaskTimeStamps) { 133 timer.update(); 134 } 135 136 jlong entry_time = timer.ticks(); 137 char* name = task->name(); 138 139 // If this is the barrier task, it can be destroyed 140 // by the GC task manager once the do_it() executes. 141 task->do_it(manager(), which()); 142 143 // Use the saved value of is_idle_task because references 144 // using "task" are not reliable for the barrier task. 145 if (!is_idle_task) { 146 manager()->note_completion(which()); 147 148 if (PrintGCTaskTimeStamps) { 149 assert(_time_stamps != NULL, 150 "Sanity (PrintGCTaskTimeStamps set late?)"); 151 152 timer.update(); 153 154 GCTaskTimeStamp* time_stamp = time_stamp_at(_time_stamp_index++); 155 156 time_stamp->set_name(name); 157 time_stamp->set_entry_time(entry_time); 158 time_stamp->set_exit_time(timer.ticks()); 159 } 160 } else { 161 // idle tasks complete outside the normal accounting 162 // so that a task can complete without waiting for idle tasks. 163 // They have to be terminated separately. 164 IdleGCTask::destroy((IdleGCTask*)task); 165 set_is_working(true); 166 } 167 168 // Check if we should release our inner resources. 169 if (manager()->should_release_resources(which())) { 170 manager()->note_release(which()); 171 break; | 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 * 24 */ 25 26 #include "precompiled.hpp" 27 #include "gc/parallel/gcTaskManager.hpp" 28 #include "gc/parallel/gcTaskThread.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 "memory/resourceArea.hpp" 34 #include "runtime/atomic.inline.hpp" 35 #include "runtime/handles.hpp" 36 #include "runtime/handles.inline.hpp" 37 #include "runtime/os.hpp" 38 #include "runtime/thread.hpp" 39 40 GCTaskThread::GCTaskThread(GCTaskManager* manager, 41 uint which, 42 uint processor_id) : 43 _manager(manager), 44 _processor_id(processor_id), 45 _time_stamps(NULL), 46 _time_stamp_index(0) 47 { 48 if (!os::create_thread(this, os::pgc_thread)) 49 vm_exit_out_of_memory(0, OOM_MALLOC_ERROR, "Cannot create GC thread. Out of system resources."); 50 51 set_id(which); 52 set_name("ParGC Thread#%d", which); 53 } 54 55 GCTaskThread::~GCTaskThread() { 56 if (_time_stamps != NULL) { 57 FREE_C_HEAP_ARRAY(GCTaskTimeStamp, _time_stamps); 58 } 59 } 60 61 void GCTaskThread::start() { 62 os::start_thread(this); 63 } 64 65 GCTaskTimeStamp* GCTaskThread::time_stamp_at(uint index) { 66 guarantee(index < GCTaskTimeStampEntries, "increase GCTaskTimeStampEntries"); 67 if (_time_stamps == NULL) { 68 // We allocate the _time_stamps array lazily since logging can be enabled dynamically 69 GCTaskTimeStamp* time_stamps = NEW_C_HEAP_ARRAY(GCTaskTimeStamp, GCTaskTimeStampEntries, mtGC); 70 void* old = Atomic::cmpxchg_ptr(time_stamps, &_time_stamps, NULL); 71 if (old != NULL) { 72 // Someone already setup the time stamps 73 FREE_C_HEAP_ARRAY(GCTaskTimeStamp, time_stamps); 74 } 75 } 76 77 return &(_time_stamps[index]); 78 } 79 80 void GCTaskThread::print_task_time_stamps() { 81 assert((Log<LOG_TAGS(gc, task, time)>::is_debug()), "Sanity"); 82 assert(_time_stamps != NULL, "Sanity"); 83 84 log_debug(gc, task, time)("GC-Thread %u entries: %d", id(), _time_stamp_index); 85 for(uint i=0; i<_time_stamp_index; i++) { 86 GCTaskTimeStamp* time_stamp = time_stamp_at(i); 87 log_debug(gc, task, time)("\t[ %s " JLONG_FORMAT " " JLONG_FORMAT " ]", 88 time_stamp->name(), 89 time_stamp->entry_time(), 90 time_stamp->exit_time()); 91 } 92 93 // Reset after dumping the data 94 _time_stamp_index = 0; 95 } 96 97 // GC workers get tasks from the GCTaskManager and execute 98 // them in this method. If there are no tasks to execute, 99 // the GC workers wait in the GCTaskManager's get_task() 100 // for tasks to be enqueued for execution. 101 102 void GCTaskThread::run() { 103 // Set up the thread for stack overflow support 104 this->record_stack_base_and_size(); 105 this->initialize_thread_local_storage(); 106 this->initialize_named_thread(); 107 // Bind yourself to your processor. 118 } 119 } 120 // Part of thread setup. 121 // ??? Are these set up once here to make subsequent ones fast? 122 HandleMark hm_outer; 123 ResourceMark rm_outer; 124 125 TimeStamp timer; 126 127 for (;/* ever */;) { 128 // These are so we can flush the resources allocated in the inner loop. 129 HandleMark hm_inner; 130 ResourceMark rm_inner; 131 for (; /* break */; ) { 132 // This will block until there is a task to be gotten. 133 GCTask* task = manager()->get_task(which()); 134 GCIdMark gc_id_mark(task->gc_id()); 135 // Record if this is an idle task for later use. 136 bool is_idle_task = task->is_idle_task(); 137 // In case the update is costly 138 if (Log<LOG_TAGS(gc, task, time)>::is_debug()) { 139 timer.update(); 140 } 141 142 jlong entry_time = timer.ticks(); 143 char* name = task->name(); 144 145 // If this is the barrier task, it can be destroyed 146 // by the GC task manager once the do_it() executes. 147 task->do_it(manager(), which()); 148 149 // Use the saved value of is_idle_task because references 150 // using "task" are not reliable for the barrier task. 151 if (!is_idle_task) { 152 manager()->note_completion(which()); 153 154 if (Log<LOG_TAGS(gc, task, time)>::is_debug()) { 155 timer.update(); 156 157 GCTaskTimeStamp* time_stamp = time_stamp_at(_time_stamp_index++); 158 159 time_stamp->set_name(name); 160 time_stamp->set_entry_time(entry_time); 161 time_stamp->set_exit_time(timer.ticks()); 162 } 163 } else { 164 // idle tasks complete outside the normal accounting 165 // so that a task can complete without waiting for idle tasks. 166 // They have to be terminated separately. 167 IdleGCTask::destroy((IdleGCTask*)task); 168 set_is_working(true); 169 } 170 171 // Check if we should release our inner resources. 172 if (manager()->should_release_resources(which())) { 173 manager()->note_release(which()); 174 break; |