1 /*
   2  * Copyright (c) 2001, 2015, 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/g1/concurrentG1Refine.hpp"
  27 #include "gc/g1/concurrentG1RefineThread.hpp"
  28 #include "gc/g1/g1CollectedHeap.inline.hpp"
  29 #include "gc/g1/g1HotCardCache.hpp"
  30 #include "runtime/java.hpp"
  31 
  32 ConcurrentG1Refine::ConcurrentG1Refine(G1CollectedHeap* g1h) :
  33   _threads(NULL),
  34   _sample_thread(NULL),
  35   _hot_card_cache(g1h)
  36 {
  37   // Ergonomically select initial concurrent refinement parameters
  38   if (FLAG_IS_DEFAULT(G1ConcRefinementGreenZone)) {
  39     FLAG_SET_DEFAULT(G1ConcRefinementGreenZone, (intx)ParallelGCThreads);
  40   }
  41   set_green_zone(G1ConcRefinementGreenZone);
  42 
  43   if (FLAG_IS_DEFAULT(G1ConcRefinementYellowZone)) {
  44     FLAG_SET_DEFAULT(G1ConcRefinementYellowZone, green_zone() * 3);
  45   }
  46   set_yellow_zone(MAX2<int>(G1ConcRefinementYellowZone, green_zone()));
  47 
  48   if (FLAG_IS_DEFAULT(G1ConcRefinementRedZone)) {
  49     FLAG_SET_DEFAULT(G1ConcRefinementRedZone, yellow_zone() * 2);
  50   }
  51   set_red_zone(MAX2<int>(G1ConcRefinementRedZone, yellow_zone()));
  52 }
  53 
  54 ConcurrentG1Refine* ConcurrentG1Refine::create(G1CollectedHeap* g1h, CardTableEntryClosure* refine_closure, jint* ecode) {
  55   ConcurrentG1Refine* cg1r = new ConcurrentG1Refine(g1h);
  56   if (cg1r == NULL) {
  57     *ecode = JNI_ENOMEM;
  58     vm_shutdown_during_initialization("Could not create ConcurrentG1Refine");
  59     return NULL;
  60   }
  61   cg1r->_n_worker_threads = thread_num();
  62 
  63   cg1r->reset_threshold_step();
  64 
  65   cg1r->_threads = NEW_C_HEAP_ARRAY_RETURN_NULL(ConcurrentG1RefineThread*, cg1r->_n_worker_threads, mtGC);
  66   if (cg1r->_threads == NULL) {
  67     *ecode = JNI_ENOMEM;
  68     vm_shutdown_during_initialization("Could not allocate an array for ConcurrentG1RefineThread");
  69     return NULL;
  70   }
  71 
  72   uint worker_id_offset = DirtyCardQueueSet::num_par_ids();
  73 
  74   ConcurrentG1RefineThread *next = NULL;
  75   for (uint i = cg1r->_n_worker_threads - 1; i != UINT_MAX; i--) {
  76     ConcurrentG1RefineThread* t = new ConcurrentG1RefineThread(cg1r, next, refine_closure, worker_id_offset, i);
  77     assert(t != NULL, "Conc refine should have been created");
  78     if (t->osthread() == NULL) {
  79       *ecode = JNI_ENOMEM;
  80       vm_shutdown_during_initialization("Could not create ConcurrentG1RefineThread");
  81       return NULL;
  82     }
  83 
  84     assert(t->cg1r() == cg1r, "Conc refine thread should refer to this");
  85     cg1r->_threads[i] = t;
  86     next = t;
  87   }
  88 
  89   cg1r->_sample_thread = new G1YoungRemSetSamplingThread();
  90   if (cg1r->_sample_thread->osthread() == NULL) {
  91     *ecode = JNI_ENOMEM;
  92     vm_shutdown_during_initialization("Could not create G1YoungRemSetSamplingThread");
  93     return NULL;
  94   }
  95 
  96   *ecode = JNI_OK;
  97   return cg1r;
  98 }
  99 
 100 void ConcurrentG1Refine::reset_threshold_step() {
 101   if (FLAG_IS_DEFAULT(G1ConcRefinementThresholdStep)) {
 102     _thread_threshold_step = (yellow_zone() - green_zone()) / (worker_thread_num() + 1);
 103   } else {
 104     _thread_threshold_step = G1ConcRefinementThresholdStep;
 105   }
 106 }
 107 
 108 void ConcurrentG1Refine::init(G1RegionToSpaceMapper* card_counts_storage) {
 109   _hot_card_cache.initialize(card_counts_storage);
 110 }
 111 
 112 void ConcurrentG1Refine::stop() {
 113   for (uint i = 0; i < _n_worker_threads; i++) {
 114     _threads[i]->stop();
 115   }
 116   _sample_thread->stop();
 117 }
 118 
 119 void ConcurrentG1Refine::reinitialize_threads() {
 120   reset_threshold_step();
 121   for (uint i = 0; i < _n_worker_threads; i++) {
 122     _threads[i]->initialize();
 123   }
 124 }
 125 
 126 ConcurrentG1Refine::~ConcurrentG1Refine() {
 127   for (uint i = 0; i < _n_worker_threads; i++) {
 128     delete _threads[i];
 129   }
 130   FREE_C_HEAP_ARRAY(ConcurrentG1RefineThread*, _threads);
 131 
 132   delete _sample_thread;
 133 }
 134 
 135 void ConcurrentG1Refine::threads_do(ThreadClosure *tc) {
 136   worker_threads_do(tc);
 137   tc->do_thread(_sample_thread);
 138 }
 139 
 140 void ConcurrentG1Refine::worker_threads_do(ThreadClosure * tc) {
 141   for (uint i = 0; i < worker_thread_num(); i++) {
 142     tc->do_thread(_threads[i]);
 143   }
 144 }
 145 
 146 uint ConcurrentG1Refine::thread_num() {
 147   return G1ConcRefinementThreads;
 148 }
 149 
 150 void ConcurrentG1Refine::print_worker_threads_on(outputStream* st) const {
 151   for (uint i = 0; i < _n_worker_threads; ++i) {
 152     _threads[i]->print_on(st);
 153     st->cr();
 154   }
 155   _sample_thread->print_on(st);
 156   st->cr();
 157 }
--- EOF ---