1 
   2 /*
   3  * Copyright (c) 2002, 2007, Oracle and/or its affiliates. All rights reserved.
   4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5  *
   6  * This code is free software; you can redistribute it and/or modify it
   7  * under the terms of the GNU General Public License version 2 only, as
   8  * published by the Free Software Foundation.
   9  *
  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 "incls/_precompiled.incl"
  27 #include "incls/_gcTaskThread.cpp.incl"
  28 
  29 GCTaskThread::GCTaskThread(GCTaskManager* manager,
  30                            uint           which,
  31                            uint           processor_id) :
  32   _manager(manager),
  33   _processor_id(processor_id),
  34   _time_stamps(NULL),
  35   _time_stamp_index(0)
  36 {
  37   if (!os::create_thread(this, os::pgc_thread))
  38     vm_exit_out_of_memory(0, "Cannot create GC thread. Out of system resources.");
  39 
  40   if (PrintGCTaskTimeStamps) {
  41     _time_stamps = NEW_C_HEAP_ARRAY(GCTaskTimeStamp, GCTaskTimeStampEntries );
  42 
  43     guarantee(_time_stamps != NULL, "Sanity");
  44   }
  45   set_id(which);
  46   set_name("GC task thread#%d (ParallelGC)", which);
  47 }
  48 
  49 GCTaskThread::~GCTaskThread() {
  50   if (_time_stamps != NULL) {
  51     FREE_C_HEAP_ARRAY(GCTaskTimeStamp, _time_stamps);
  52   }
  53 }
  54 
  55 void GCTaskThread::start() {
  56   os::start_thread(this);
  57 }
  58 
  59 GCTaskTimeStamp* GCTaskThread::time_stamp_at(uint index) {
  60   guarantee(index < GCTaskTimeStampEntries, "increase GCTaskTimeStampEntries");
  61 
  62   return &(_time_stamps[index]);
  63 }
  64 
  65 void GCTaskThread::print_task_time_stamps() {
  66   assert(PrintGCTaskTimeStamps, "Sanity");
  67   assert(_time_stamps != NULL, "Sanity (Probably set PrintGCTaskTimeStamps late)");
  68 
  69   tty->print_cr("GC-Thread %u entries: %d", id(), _time_stamp_index);
  70   for(uint i=0; i<_time_stamp_index; i++) {
  71     GCTaskTimeStamp* time_stamp = time_stamp_at(i);
  72     tty->print_cr("\t[ %s " INT64_FORMAT " " INT64_FORMAT " ]",
  73                   time_stamp->name(),
  74                   time_stamp->entry_time(),
  75                   time_stamp->exit_time());
  76   }
  77 
  78   // Reset after dumping the data
  79   _time_stamp_index = 0;
  80 }
  81 
  82 void GCTaskThread::print_on(outputStream* st) const {
  83   st->print("\"%s\" ", name());
  84   Thread::print_on(st);
  85   st->cr();
  86 }
  87 
  88 void GCTaskThread::run() {
  89   // Set up the thread for stack overflow support
  90   this->record_stack_base_and_size();
  91   this->initialize_thread_local_storage();
  92   // Bind yourself to your processor.
  93   if (processor_id() != GCTaskManager::sentinel_worker()) {
  94     if (TraceGCTaskThread) {
  95       tty->print_cr("GCTaskThread::run: "
  96                     "  binding to processor %u", processor_id());
  97     }
  98     if (!os::bind_to_processor(processor_id())) {
  99       DEBUG_ONLY(
 100         warning("Couldn't bind GCTaskThread %u to processor %u",
 101                       which(), processor_id());
 102       )
 103     }
 104   }
 105   // Part of thread setup.
 106   // ??? Are these set up once here to make subsequent ones fast?
 107   HandleMark   hm_outer;
 108   ResourceMark rm_outer;
 109 
 110   TimeStamp timer;
 111 
 112   for (;/* ever */;) {
 113     // These are so we can flush the resources allocated in the inner loop.
 114     HandleMark   hm_inner;
 115     ResourceMark rm_inner;
 116     for (; /* break */; ) {
 117       // This will block until there is a task to be gotten.
 118       GCTask* task = manager()->get_task(which());
 119 
 120       // In case the update is costly
 121       if (PrintGCTaskTimeStamps) {
 122         timer.update();
 123       }
 124 
 125       jlong entry_time = timer.ticks();
 126       char* name = task->name();
 127 
 128       task->do_it(manager(), which());
 129       manager()->note_completion(which());
 130 
 131       if (PrintGCTaskTimeStamps) {
 132         assert(_time_stamps != NULL, "Sanity (PrintGCTaskTimeStamps set late?)");
 133 
 134         timer.update();
 135 
 136         GCTaskTimeStamp* time_stamp = time_stamp_at(_time_stamp_index++);
 137 
 138         time_stamp->set_name(name);
 139         time_stamp->set_entry_time(entry_time);
 140         time_stamp->set_exit_time(timer.ticks());
 141       }
 142 
 143       // Check if we should release our inner resources.
 144       if (manager()->should_release_resources(which())) {
 145         manager()->note_release(which());
 146         break;
 147       }
 148     }
 149   }
 150 }