1 /*
   2  * Copyright (c) 2017, Red Hat, Inc. and/or its affiliates.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #include "precompiled.hpp"
  25 
  26 #include "gc_implementation/shared/adaptiveSizePolicy.hpp"
  27 #include "gc_implementation/shenandoah/shenandoahWorkerPolicy.hpp"
  28 #include "runtime/thread.hpp"
  29 
  30 uint ShenandoahWorkerPolicy::_prev_par_marking     = 0;
  31 uint ShenandoahWorkerPolicy::_prev_conc_marking    = 0;
  32 uint ShenandoahWorkerPolicy::_prev_conc_evac       = 0;
  33 uint ShenandoahWorkerPolicy::_prev_fullgc          = 0;
  34 uint ShenandoahWorkerPolicy::_prev_degengc         = 0;
  35 uint ShenandoahWorkerPolicy::_prev_conc_update_ref = 0;
  36 uint ShenandoahWorkerPolicy::_prev_par_update_ref  = 0;
  37 uint ShenandoahWorkerPolicy::_prev_conc_cleanup    = 0;
  38 
  39 uint ShenandoahWorkerPolicy::calc_workers_for_init_marking() {
  40   uint active_workers = (_prev_par_marking == 0) ? ParallelGCThreads : _prev_par_marking;
  41 
  42   _prev_par_marking =
  43     AdaptiveSizePolicy::calc_active_workers(ParallelGCThreads,
  44                                             active_workers,
  45                                             Threads::number_of_non_daemon_threads());
  46   return _prev_par_marking;
  47 }
  48 
  49 uint ShenandoahWorkerPolicy::calc_workers_for_conc_marking() {
  50   uint active_workers = (_prev_conc_marking == 0) ?  ConcGCThreads : _prev_conc_marking;
  51   _prev_conc_marking =
  52     AdaptiveSizePolicy::calc_active_conc_workers(ConcGCThreads,
  53                                                  active_workers,
  54                                                  Threads::number_of_non_daemon_threads());
  55   return _prev_conc_marking;
  56 }
  57 
  58 // Reuse the calculation result from init marking
  59 uint ShenandoahWorkerPolicy::calc_workers_for_final_marking() {
  60   return _prev_par_marking;
  61 }
  62 
  63 // Calculate workers for concurrent evacuation (concurrent GC)
  64 uint ShenandoahWorkerPolicy::calc_workers_for_conc_evac() {
  65   uint active_workers = (_prev_conc_evac == 0) ? ConcGCThreads : _prev_conc_evac;
  66   _prev_conc_evac =
  67     AdaptiveSizePolicy::calc_active_conc_workers(ConcGCThreads,
  68                                                  active_workers,
  69                                                  Threads::number_of_non_daemon_threads());
  70   return _prev_conc_evac;
  71 }
  72 
  73 // Calculate workers for parallel fullgc
  74 uint ShenandoahWorkerPolicy::calc_workers_for_fullgc() {
  75   uint active_workers = (_prev_fullgc == 0) ?  ParallelGCThreads : _prev_fullgc;
  76   _prev_fullgc =
  77     AdaptiveSizePolicy::calc_active_workers(ParallelGCThreads,
  78                                             active_workers,
  79                                             Threads::number_of_non_daemon_threads());
  80   return _prev_fullgc;
  81 }
  82 
  83 // Calculate workers for concurrent reference update
  84 uint ShenandoahWorkerPolicy::calc_workers_for_conc_update_ref() {
  85   uint active_workers = (_prev_conc_update_ref == 0) ? ConcGCThreads : _prev_conc_update_ref;
  86   _prev_conc_update_ref =
  87     AdaptiveSizePolicy::calc_active_conc_workers(ConcGCThreads,
  88                                                  active_workers,
  89                                                  Threads::number_of_non_daemon_threads());
  90   return _prev_conc_update_ref;
  91 }
  92 
  93 // Calculate workers for parallel reference update
  94 uint ShenandoahWorkerPolicy::calc_workers_for_final_update_ref() {
  95   uint active_workers = (_prev_par_update_ref == 0) ? ParallelGCThreads : _prev_par_update_ref;
  96   _prev_par_update_ref =
  97     AdaptiveSizePolicy::calc_active_workers(ParallelGCThreads,
  98                                             active_workers,
  99                                             Threads::number_of_non_daemon_threads());
 100   return _prev_par_update_ref;
 101 }
 102 
 103 // Calculate workers for parallel degenerated gc
 104 uint ShenandoahWorkerPolicy::calc_workers_for_stw_degenerated() {
 105   uint active_workers = (_prev_degengc == 0) ?  ParallelGCThreads : _prev_degengc;
 106   _prev_degengc =
 107     AdaptiveSizePolicy::calc_active_workers(ParallelGCThreads,
 108                                             active_workers,
 109                                             Threads::number_of_non_daemon_threads());
 110   return _prev_degengc;
 111 }
 112 
 113 uint ShenandoahWorkerPolicy::calc_workers_for_conc_preclean() {
 114   return _prev_conc_marking;
 115 }
 116 
 117 uint ShenandoahWorkerPolicy::calc_workers_for_conc_cleanup() {
 118   uint active_workers = (_prev_conc_cleanup == 0) ? ConcGCThreads : _prev_conc_cleanup;
 119   _prev_conc_cleanup =
 120           AdaptiveSizePolicy::calc_active_conc_workers(ConcGCThreads,
 121                                                        active_workers,
 122                                                        Threads::number_of_non_daemon_threads());
 123   return _prev_conc_cleanup;
 124 }