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