1 /*
   2  * Copyright (c) 2001, 2013, 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_implementation/g1/concurrentG1Refine.hpp"
  27 #include "gc_implementation/g1/concurrentG1RefineThread.hpp"
  28 #include "gc_implementation/g1/g1CollectedHeap.inline.hpp"
  29 #include "gc_implementation/g1/g1HotCardCache.hpp"
  30 
  31 ConcurrentG1Refine::ConcurrentG1Refine(G1CollectedHeap* g1h) :
  32   _threads(NULL), _n_threads(0),
  33   _hot_card_cache(g1h)
  34 {
  35   // Ergomonically select initial concurrent refinement parameters
  36   if (FLAG_IS_DEFAULT(G1ConcRefinementGreenZone)) {
  37     FLAG_SET_DEFAULT(G1ConcRefinementGreenZone, MAX2<int>(ParallelGCThreads, 1));
  38   }
  39   set_green_zone(G1ConcRefinementGreenZone);
  40 
  41   if (FLAG_IS_DEFAULT(G1ConcRefinementYellowZone)) {
  42     FLAG_SET_DEFAULT(G1ConcRefinementYellowZone, green_zone() * 3);
  43   }
  44   set_yellow_zone(MAX2<int>(G1ConcRefinementYellowZone, green_zone()));
  45 
  46   if (FLAG_IS_DEFAULT(G1ConcRefinementRedZone)) {
  47     FLAG_SET_DEFAULT(G1ConcRefinementRedZone, yellow_zone() * 2);
  48   }
  49   set_red_zone(MAX2<int>(G1ConcRefinementRedZone, yellow_zone()));
  50 
  51   _n_worker_threads = thread_num();
  52   // We need one extra thread to do the young gen rset size sampling.
  53   _n_threads = _n_worker_threads + 1;
  54 
  55   reset_threshold_step();
  56 
  57   _threads = NEW_C_HEAP_ARRAY(ConcurrentG1RefineThread*, _n_threads, mtGC);
  58 
  59   int worker_id_offset = (int)DirtyCardQueueSet::num_par_ids();
  60 
  61   ConcurrentG1RefineThread *next = NULL;
  62   for (int i = _n_threads - 1; i >= 0; i--) {
  63     ConcurrentG1RefineThread* t = new ConcurrentG1RefineThread(this, next, worker_id_offset, i);
  64     assert(t != NULL, "Conc refine should have been created");
  65     assert(t->cg1r() == this, "Conc refine thread should refer to this");
  66     _threads[i] = t;
  67     next = t;
  68   }
  69 }
  70 
  71 void ConcurrentG1Refine::reset_threshold_step() {
  72   if (FLAG_IS_DEFAULT(G1ConcRefinementThresholdStep)) {
  73     _thread_threshold_step = (yellow_zone() - green_zone()) / (worker_thread_num() + 1);
  74   } else {
  75     _thread_threshold_step = G1ConcRefinementThresholdStep;
  76   }
  77 }
  78 
  79 void ConcurrentG1Refine::init() {
  80   _hot_card_cache.initialize();
  81 }
  82 
  83 void ConcurrentG1Refine::stop() {
  84   if (_threads != NULL) {
  85     for (int i = 0; i < _n_threads; i++) {
  86       _threads[i]->stop();
  87     }
  88   }
  89 }
  90 
  91 void ConcurrentG1Refine::reinitialize_threads() {
  92   reset_threshold_step();
  93   if (_threads != NULL) {
  94     for (int i = 0; i < _n_threads; i++) {
  95       _threads[i]->initialize();
  96     }
  97   }
  98 }
  99 
 100 ConcurrentG1Refine::~ConcurrentG1Refine() {
 101   if (_threads != NULL) {
 102     for (int i = 0; i < _n_threads; i++) {
 103       delete _threads[i];
 104     }
 105     FREE_C_HEAP_ARRAY(ConcurrentG1RefineThread*, _threads, mtGC);
 106   }
 107 }
 108 
 109 void ConcurrentG1Refine::threads_do(ThreadClosure *tc) {
 110   if (_threads != NULL) {
 111     for (int i = 0; i < _n_threads; i++) {
 112       tc->do_thread(_threads[i]);
 113     }
 114   }
 115 }
 116 
 117 int ConcurrentG1Refine::thread_num() {
 118   int n_threads = (G1ConcRefinementThreads > 0) ? G1ConcRefinementThreads
 119                                                 : ParallelGCThreads;
 120   return MAX2<int>(n_threads, 1);
 121 }
 122 
 123 void ConcurrentG1Refine::print_worker_threads_on(outputStream* st) const {
 124   for (int i = 0; i < _n_threads; ++i) {
 125     _threads[i]->print_on(st);
 126     st->cr();
 127   }
 128 }