1 /*
   2  * Copyright (c) 2015, 2019, Red Hat, Inc. All rights reserved.
   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 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_HPP
  26 
  27 #include "code/codeCache.hpp"
  28 #include "gc/shared/oopStorageParState.hpp"
  29 #include "gc/shenandoah/shenandoahCodeRoots.hpp"
  30 #include "gc/shenandoah/shenandoahHeap.hpp"
  31 #include "gc/shenandoah/shenandoahPhaseTimings.hpp"
  32 #include "gc/shared/strongRootsScope.hpp"
  33 #include "gc/shared/weakProcessor.hpp"
  34 #include "gc/shared/weakProcessorPhaseTimes.hpp"
  35 #include "gc/shared/workgroup.hpp"
  36 #include "memory/allocation.hpp"
  37 #include "memory/iterator.hpp"
  38 
  39 class ParallelCLDRootIterator {
  40 public:
  41   ParallelCLDRootIterator();
  42   void root_cld_do(CLDClosure* strong, CLDClosure* weak);
  43 };
  44 
  45 enum Shenandoah_process_roots_tasks {
  46   SHENANDOAH_RP_PS_Universe_oops_do,
  47   SHENANDOAH_RP_PS_JNIHandles_oops_do,
  48   SHENANDOAH_RP_PS_ObjectSynchronizer_oops_do,
  49   SHENANDOAH_RP_PS_Management_oops_do,
  50   SHENANDOAH_RP_PS_SystemDictionary_oops_do,
  51   SHENANDOAH_RP_PS_jvmti_oops_do,
  52   // Leave this one last.
  53   SHENANDOAH_RP_PS_NumElements
  54 };
  55 
  56 class ShenandoahRootProcessor : public StackObj {
  57   SubTasksDone* _process_strong_tasks;
  58   StrongRootsScope _srs;
  59   ShenandoahPhaseTimings::Phase _phase;
  60   ParallelCLDRootIterator _cld_iterator;
  61   ShenandoahAllCodeRootsIterator _coderoots_all_iterator;
  62   CodeBlobClosure* _threads_nmethods_cl;
  63   WeakProcessorPhaseTimes _weak_processor_timings;
  64   WeakProcessor::Task     _weak_processor_task;
  65   bool                    _processed_weak_roots;
  66 
  67   void process_java_roots(OopClosure* scan_non_heap_roots,
  68                           CLDClosure* scan_strong_clds,
  69                           CLDClosure* scan_weak_clds,
  70                           CodeBlobClosure* scan_strong_code,
  71                           ThreadClosure* thread_cl,
  72                           uint worker_i);
  73 
  74   void process_vm_roots(OopClosure* scan_non_heap_roots,
  75                         uint worker_i);
  76 
  77   void weak_processor_timing_to_shenandoah_timing(const WeakProcessorPhases::Phase wpp,
  78                                                   const ShenandoahPhaseTimings::GCParPhases spp,
  79                                                   ShenandoahWorkerTimings* worker_times) const;
  80 
  81 public:
  82   ShenandoahRootProcessor(ShenandoahHeap* heap, uint n_workers,
  83                           ShenandoahPhaseTimings::Phase phase);
  84   ~ShenandoahRootProcessor();
  85 
  86   // Apply oops, clds and blobs to all strongly reachable roots in the system.
  87   // Optionally, apply class loader closure to weak clds, depending on class unloading
  88   // for the particular GC cycles.
  89   void process_strong_roots(OopClosure* oops,
  90                             CLDClosure* clds,
  91                             CodeBlobClosure* blobs,
  92                             ThreadClosure* thread_cl,
  93                             uint worker_id);
  94 
  95   // Apply oops, clds and blobs to strongly reachable roots in the system
  96   void process_all_roots(OopClosure* oops,
  97                          CLDClosure* clds,
  98                          CodeBlobClosure* blobs,
  99                          ThreadClosure* thread_cl,
 100                          uint worker_id);
 101 
 102   // Apply oops, clds and blobs to strongly and weakly reachable roots in the system
 103   template <typename IsAlive>
 104   void update_all_roots(OopClosure* oops,
 105                         CLDClosure* clds,
 106                         CodeBlobClosure* blobs,
 107                         ThreadClosure* thread_cl,
 108                         uint worker_id);
 109 
 110   // For slow debug/verification code
 111   void process_all_roots_slow(OopClosure* oops);
 112 
 113   // Number of worker threads used by the root processor.
 114   uint n_workers() const;
 115 };
 116 
 117 class ShenandoahRootEvacuator : public StackObj {
 118   SubTasksDone* _evacuation_tasks;
 119   StrongRootsScope _srs;
 120   ShenandoahPhaseTimings::Phase _phase;
 121   ShenandoahCsetCodeRootsIterator _coderoots_cset_iterator;
 122 
 123   enum Shenandoah_evacuate_roots_tasks {
 124     SHENANDOAH_EVAC_Universe_oops_do,
 125     SHENANDOAH_EVAC_ObjectSynchronizer_oops_do,
 126     SHENANDOAH_EVAC_Management_oops_do,
 127     SHENANDOAH_EVAC_SystemDictionary_oops_do,
 128     SHENANDOAH_EVAC_jvmti_oops_do,
 129     // Leave this one last.
 130     SHENANDOAH_EVAC_NumElements
 131   };
 132 public:
 133   ShenandoahRootEvacuator(ShenandoahHeap* heap, uint n_workers,
 134                           ShenandoahPhaseTimings::Phase phase);
 135   ~ShenandoahRootEvacuator();
 136 
 137   void process_evacuate_roots(OopClosure* oops,
 138                               CodeBlobClosure* blobs,
 139                               uint worker_id);
 140 
 141   // Number of worker threads used by the root processor.
 142   uint n_workers() const;
 143 };
 144 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHROOTPROCESSOR_HPP