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/shared/adaptiveSizePolicy.hpp"
  27 #include "gc/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_stw_traversal   = 0;
  36 uint ShenandoahWorkerPolicy::_prev_conc_traversal  = 0;
  37 uint ShenandoahWorkerPolicy::_prev_conc_update_ref = 0;
  38 uint ShenandoahWorkerPolicy::_prev_par_update_ref  = 0;
  39 uint ShenandoahWorkerPolicy::_prev_conc_cleanup    = 0;
  40 
  41 uint ShenandoahWorkerPolicy::calc_workers_for_init_marking() {
  42   uint active_workers = (_prev_par_marking == 0) ? ParallelGCThreads : _prev_par_marking;
  43 
  44   _prev_par_marking =
  45     AdaptiveSizePolicy::calc_active_workers(ParallelGCThreads,
  46                                             active_workers,
  47                                             Threads::number_of_non_daemon_threads());
  48   return _prev_par_marking;
  49 }
  50 
  51 uint ShenandoahWorkerPolicy::calc_workers_for_conc_marking() {
  52   uint active_workers = (_prev_conc_marking == 0) ?  ConcGCThreads : _prev_conc_marking;
  53   _prev_conc_marking =
  54     AdaptiveSizePolicy::calc_active_conc_workers(ConcGCThreads,
  55                                                  active_workers,
  56                                                  Threads::number_of_non_daemon_threads());
  57   return _prev_conc_marking;
  58 }
  59 
  60 // Reuse the calculation result from init marking
  61 uint ShenandoahWorkerPolicy::calc_workers_for_final_marking() {
  62   return _prev_par_marking;
  63 }
  64 
  65 // Calculate workers for concurrent evacuation (concurrent GC)
  66 uint ShenandoahWorkerPolicy::calc_workers_for_conc_evac() {
  67   uint active_workers = (_prev_conc_evac == 0) ? ConcGCThreads : _prev_conc_evac;
  68   _prev_conc_evac =
  69     AdaptiveSizePolicy::calc_active_workers(ConcGCThreads,
  70                                             active_workers,
  71                                             Threads::number_of_non_daemon_threads());
  72   return _prev_conc_evac;
  73 }
  74 
  75 // Calculate workers for parallel fullgc
  76 uint ShenandoahWorkerPolicy::calc_workers_for_fullgc() {
  77   uint active_workers = (_prev_fullgc == 0) ?  ParallelGCThreads : _prev_fullgc;
  78   _prev_fullgc =
  79     AdaptiveSizePolicy::calc_active_workers(ParallelGCThreads,
  80                                             active_workers,
  81                                             Threads::number_of_non_daemon_threads());
  82   return _prev_fullgc;
  83 }
  84 
  85 // Calculate workers for parallel degenerated gc
  86 uint ShenandoahWorkerPolicy::calc_workers_for_stw_degenerated() {
  87   uint active_workers = (_prev_degengc == 0) ?  ParallelGCThreads : _prev_degengc;
  88   _prev_degengc =
  89     AdaptiveSizePolicy::calc_active_workers(ParallelGCThreads,
  90                                             active_workers,
  91                                             Threads::number_of_non_daemon_threads());
  92   return _prev_degengc;
  93 }
  94 
  95 // Calculate workers for Stop-the-world traversal GC
  96 uint ShenandoahWorkerPolicy::calc_workers_for_stw_traversal() {
  97   uint active_workers = (_prev_stw_traversal == 0) ? ParallelGCThreads : _prev_stw_traversal;
  98   _prev_stw_traversal =
  99     AdaptiveSizePolicy::calc_active_workers(ParallelGCThreads,
 100                                             active_workers,
 101                                             Threads::number_of_non_daemon_threads());
 102   return _prev_stw_traversal;
 103 }
 104 
 105 // Calculate workers for concurent traversal GC
 106 uint ShenandoahWorkerPolicy::calc_workers_for_conc_traversal() {
 107   uint active_workers = (_prev_conc_traversal == 0) ? ConcGCThreads : _prev_conc_traversal;
 108   _prev_conc_traversal =
 109     AdaptiveSizePolicy::calc_active_conc_workers(ConcGCThreads,
 110                                                  active_workers,
 111                                                  Threads::number_of_non_daemon_threads());
 112   return _prev_conc_traversal;
 113 }
 114 
 115 // Calculate workers for concurrent reference update
 116 uint ShenandoahWorkerPolicy::calc_workers_for_conc_update_ref() {
 117   uint active_workers = (_prev_conc_update_ref == 0) ? ConcGCThreads : _prev_conc_update_ref;
 118   _prev_conc_update_ref =
 119     AdaptiveSizePolicy::calc_active_workers(ConcGCThreads,
 120                                             active_workers,
 121                                             Threads::number_of_non_daemon_threads());
 122   return _prev_conc_update_ref;
 123 }
 124 
 125 // Calculate workers for parallel reference update
 126 uint ShenandoahWorkerPolicy::calc_workers_for_final_update_ref() {
 127   uint active_workers = (_prev_par_update_ref == 0) ? ParallelGCThreads : _prev_par_update_ref;
 128   _prev_par_update_ref =
 129     AdaptiveSizePolicy::calc_active_workers(ParallelGCThreads,
 130                                             active_workers,
 131                                             Threads::number_of_non_daemon_threads());
 132   return _prev_par_update_ref;
 133 }
 134 
 135 uint ShenandoahWorkerPolicy::calc_workers_for_conc_preclean() {
 136   return _prev_conc_marking;
 137 }
 138 
 139 uint ShenandoahWorkerPolicy::calc_workers_for_conc_cleanup() {
 140   uint active_workers = (_prev_conc_cleanup == 0) ? ConcGCThreads : _prev_conc_cleanup;
 141   _prev_conc_cleanup =
 142           AdaptiveSizePolicy::calc_active_workers(ConcGCThreads,
 143                                                   active_workers,
 144                                                   Threads::number_of_non_daemon_threads());
 145   return _prev_conc_cleanup;
 146 }