1 /*
   2  * Copyright (c) 2002, 2016, 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  */
  24 
  25 #include "precompiled.hpp"
  26 #include "gc/parallel/gcTaskManager.hpp"
  27 #include "gc/parallel/gcTaskThread.hpp"
  28 #include "gc/shared/gcId.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/allocation.hpp"
  31 #include "memory/allocation.inline.hpp"
  32 #include "memory/resourceArea.hpp"
  33 #include "runtime/atomic.inline.hpp"
  34 #include "runtime/handles.hpp"
  35 #include "runtime/handles.inline.hpp"
  36 #include "runtime/os.hpp"
  37 #include "runtime/thread.hpp"
  38 
  39 #define PARGCTHREAD "ParGC Thread"
  40 
  41 GCTaskThread::GCTaskThread(GCTaskManager* manager,
  42                            uint           which,
  43                            uint           processor_id) :
  44   _manager(manager),
  45   _processor_id(processor_id),
  46   _time_stamps(NULL),
  47   _time_stamp_index(0)
  48 {
  49   set_id(which);
  50   set_name(PARGCTHREAD "#%d", which);
  51 }
  52 
  53 GCTaskThread::~GCTaskThread() {
  54   if (_time_stamps != NULL) {
  55     FREE_C_HEAP_ARRAY(GCTaskTimeStamp, _time_stamps);
  56   }
  57 }
  58 
  59 const char* GCTaskThread::task_name() { return PARGCTHREAD; }
  60 
  61 GCTaskTimeStamp* GCTaskThread::time_stamp_at(uint index) {
  62   guarantee(index < GCTaskTimeStampEntries, "increase GCTaskTimeStampEntries");
  63   if (_time_stamps == NULL) {
  64     // We allocate the _time_stamps array lazily since logging can be enabled dynamically
  65     GCTaskTimeStamp* time_stamps = NEW_C_HEAP_ARRAY(GCTaskTimeStamp, GCTaskTimeStampEntries, mtGC);
  66     void* old = Atomic::cmpxchg_ptr(time_stamps, &_time_stamps, NULL);
  67     if (old != NULL) {
  68       // Someone already setup the time stamps
  69       FREE_C_HEAP_ARRAY(GCTaskTimeStamp, time_stamps);
  70     }
  71   }
  72 
  73   return &(_time_stamps[index]);
  74 }
  75 
  76 void GCTaskThread::print_task_time_stamps() {
  77   assert(log_is_enabled(Debug, gc, task, time), "Sanity");
  78 
  79   // Since _time_stamps is now lazily allocated we need to check that it
  80   // has in fact been allocated when calling this function.
  81   if (_time_stamps != NULL) {
  82     log_debug(gc, task, time)("GC-Thread %u entries: %d", id(), _time_stamp_index);
  83     for(uint i=0; i<_time_stamp_index; i++) {
  84       GCTaskTimeStamp* time_stamp = time_stamp_at(i);
  85       log_debug(gc, task, time)("\t[ %s " JLONG_FORMAT " " JLONG_FORMAT " ]",
  86                                 time_stamp->name(),
  87                                 time_stamp->entry_time(),
  88                                 time_stamp->exit_time());
  89     }
  90 
  91     // Reset after dumping the data
  92     _time_stamp_index = 0;
  93   }
  94 }
  95 
  96 // GC workers get tasks from the GCTaskManager and execute
  97 // them in this method.  If there are no tasks to execute,
  98 // the GC workers wait in the GCTaskManager's get_task()
  99 // for tasks to be enqueued for execution.
 100 
 101 void GCTaskThread::run() {
 102   // Set up the thread for stack overflow support
 103   this->record_stack_base_and_size();
 104   this->initialize_named_thread();
 105   // Bind yourself to your processor.
 106   if (processor_id() != GCTaskManager::sentinel_worker()) {
 107     log_trace(gc, task, thread)("GCTaskThread::run: binding to processor %u", processor_id());
 108     if (!os::bind_to_processor(processor_id())) {
 109       DEBUG_ONLY(
 110         log_warning(gc)("Couldn't bind GCTaskThread %u to processor %u",
 111                         which(), processor_id());
 112       )
 113     }
 114   }
 115   // Part of thread setup.
 116   // ??? Are these set up once here to make subsequent ones fast?
 117   HandleMark   hm_outer;
 118   ResourceMark rm_outer;
 119 
 120   TimeStamp timer;
 121 
 122   for (;/* ever */;) {
 123     // These are so we can flush the resources allocated in the inner loop.
 124     HandleMark   hm_inner;
 125     ResourceMark rm_inner;
 126     for (; /* break */; ) {
 127       // This will block until there is a task to be gotten.
 128       GCTask* task = manager()->get_task(which());
 129       GCIdMark gc_id_mark(task->gc_id());
 130       // Record if this is an idle task for later use.
 131       bool is_idle_task = task->is_idle_task();
 132       // In case the update is costly
 133       if (log_is_enabled(Debug, gc, task, time)) {
 134         timer.update();
 135       }
 136 
 137       jlong entry_time = timer.ticks();
 138       char* name = task->name();
 139 
 140       // If this is the barrier task, it can be destroyed
 141       // by the GC task manager once the do_it() executes.
 142       task->do_it(manager(), which());
 143 
 144       // Use the saved value of is_idle_task because references
 145       // using "task" are not reliable for the barrier task.
 146       if (!is_idle_task) {
 147         manager()->note_completion(which());
 148 
 149         if (log_is_enabled(Debug, gc, task, time)) {
 150           timer.update();
 151 
 152           GCTaskTimeStamp* time_stamp = time_stamp_at(_time_stamp_index);
 153 
 154           time_stamp->set_name(name);
 155           time_stamp->set_entry_time(entry_time);
 156           time_stamp->set_exit_time(timer.ticks());
 157 
 158           // Update the index after we have set up the entry correctly since
 159           // GCTaskThread::print_task_time_stamps() may read this value concurrently.
 160           _time_stamp_index++;
 161         }
 162       } else {
 163         // idle tasks complete outside the normal accounting
 164         // so that a task can complete without waiting for idle tasks.
 165         // They have to be terminated separately.
 166         IdleGCTask::destroy((IdleGCTask*)task);
 167         set_is_working(true);
 168       }
 169 
 170       // Check if we should release our inner resources.
 171       if (manager()->should_release_resources(which())) {
 172         manager()->note_release(which());
 173         break;
 174       }
 175     }
 176   }
 177 }