1 /*
   2  * Copyright (c) 2017, 2019, 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 "classfile/stringTable.hpp"
  27 #include "gc/shared/oopStorage.inline.hpp"
  28 #include "gc/shared/oopStorageParState.inline.hpp"
  29 #include "gc/shared/weakProcessor.inline.hpp"
  30 #include "gc/shared/weakProcessorPhases.hpp"
  31 #include "gc/shared/weakProcessorPhaseTimes.hpp"
  32 #include "memory/allocation.inline.hpp"
  33 #include "memory/iterator.hpp"
  34 #include "runtime/globals.hpp"
  35 #include "utilities/macros.hpp"
  36 
  37 void WeakProcessor::weak_oops_do(BoolObjectClosure* is_alive, OopClosure* keep_alive) {
  38   StringTable::reset_dead_counter();
  39   CountingIsAliveClosure<BoolObjectClosure> cl(is_alive);
  40   FOR_EACH_WEAK_PROCESSOR_PHASE(phase) {
  41     if (WeakProcessorPhases::is_serial(phase)) {
  42       WeakProcessorPhases::processor(phase)(&cl, keep_alive);
  43     } else {
  44       WeakProcessorPhases::oop_storage(phase)->weak_oops_do(&cl, keep_alive);
  45     }
  46     if (WeakProcessorPhases::is_stringtable(phase)) {
  47       StringTable::inc_dead_counter(cl.num_dead());
  48     }
  49   }
  50   StringTable::finish_dead_counter();
  51 }
  52 
  53 void WeakProcessor::oops_do(OopClosure* closure) {
  54   AlwaysTrueClosure always_true;
  55   weak_oops_do(&always_true, closure);
  56 }
  57 
  58 uint WeakProcessor::ergo_workers(uint max_workers) {
  59   // Ignore ParallelRefProcEnabled; that's for j.l.r.Reference processing.
  60   if (ReferencesPerThread == 0) {
  61     // Configuration says always use all the threads.
  62     return max_workers;
  63   }
  64 
  65   // One thread per ReferencesPerThread references (or fraction thereof)
  66   // in the various OopStorage objects, bounded by max_threads.
  67   //
  68   // Serial phases are ignored in this calculation, because of the
  69   // cost of running unnecessary threads.  These phases are normally
  70   // small or empty (assuming they are configured to exist at all),
  71   // and development oriented, so not allocating any threads
  72   // specifically for them is okay.
  73   size_t ref_count = 0;
  74   FOR_EACH_WEAK_PROCESSOR_OOP_STORAGE_PHASE(phase) {
  75     ref_count += WeakProcessorPhases::oop_storage(phase)->allocation_count();
  76   }
  77 
  78   // +1 to (approx) round up the ref per thread division.
  79   size_t nworkers = 1 + (ref_count / ReferencesPerThread);
  80   nworkers = MIN2(nworkers, static_cast<size_t>(max_workers));
  81   return static_cast<uint>(nworkers);
  82 }
  83 
  84 void WeakProcessor::Task::initialize() {
  85   assert(_nworkers != 0, "must be");
  86   assert(_phase_times == NULL || _nworkers <= _phase_times->max_threads(),
  87          "nworkers (%u) exceeds max threads (%u)",
  88          _nworkers, _phase_times->max_threads());
  89 
  90   if (_phase_times) {
  91     _phase_times->set_active_workers(_nworkers);
  92   }
  93 
  94   uint storage_count = WeakProcessorPhases::oop_storage_phase_count;
  95   _storage_states = NEW_C_HEAP_ARRAY(StorageState, storage_count, mtGC);
  96 
  97   StorageState* states = _storage_states;
  98   FOR_EACH_WEAK_PROCESSOR_OOP_STORAGE_PHASE(phase) {
  99     OopStorage* storage = WeakProcessorPhases::oop_storage(phase);
 100     new (states++) StorageState(storage, _nworkers);
 101   }
 102   StringTable::reset_dead_counter();
 103 }
 104 
 105 WeakProcessor::Task::Task(uint nworkers) :
 106   _phase_times(NULL),
 107   _nworkers(nworkers),
 108   _serial_phases_done(WeakProcessorPhases::serial_phase_count),
 109   _storage_states(NULL)
 110 {
 111   initialize();
 112 }
 113 
 114 WeakProcessor::Task::Task(WeakProcessorPhaseTimes* phase_times, uint nworkers) :
 115   _phase_times(phase_times),
 116   _nworkers(nworkers),
 117   _serial_phases_done(WeakProcessorPhases::serial_phase_count),
 118   _storage_states(NULL)
 119 {
 120   initialize();
 121 }
 122 
 123 WeakProcessor::Task::~Task() {
 124   if (_storage_states != NULL) {
 125     StorageState* states = _storage_states;
 126     FOR_EACH_WEAK_PROCESSOR_OOP_STORAGE_PHASE(phase) {
 127       states->StorageState::~StorageState();
 128       ++states;
 129     }
 130     FREE_C_HEAP_ARRAY(StorageState, _storage_states);
 131   }
 132   StringTable::finish_dead_counter();
 133 }
 134 
 135 void WeakProcessor::GangTask::work(uint worker_id) {
 136   _erased_do_work(this, worker_id);
 137 }