1 /*
   2  * Copyright (c) 2018, 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 #ifndef SHARE_VM_GC_SHARED_PARALLELCLEANING_HPP
  26 #define SHARE_VM_GC_SHARED_PARALLELCLEANING_HPP
  27 
  28 #include "classfile/classLoaderDataGraph.inline.hpp"
  29 #include "gc/shared/oopStorageParState.hpp"
  30 #include "gc/shared/stringdedup/stringDedup.hpp"
  31 #include "gc/shared/workgroup.hpp"
  32 
  33 class ParallelCleaningTask;
  34 
  35 class StringCleaningTask : public AbstractGangTask {
  36 private:
  37   BoolObjectClosure* _is_alive;
  38   StringDedupUnlinkOrOopsDoClosure * const _dedup_closure;
  39 
  40   OopStorage::ParState<false /* concurrent */, false /* const */> _par_state_string;
  41 
  42   int _initial_string_table_size;
  43 
  44   bool            _process_strings;
  45   volatile size_t _strings_processed;
  46   volatile size_t _strings_removed;
  47 
  48 public:
  49   StringCleaningTask(BoolObjectClosure* is_alive, StringDedupUnlinkOrOopsDoClosure* dedup_closure, bool process_strings);
  50   ~StringCleaningTask();
  51 
  52   void work(uint worker_id);
  53 
  54   size_t strings_processed() const { return _strings_processed; }
  55   size_t strings_removed()   const { return _strings_removed; }
  56 };
  57 
  58 class CodeCacheUnloadingTask {
  59   static Monitor* _lock;
  60 
  61   CodeCache::UnloadingScope _unloading_scope;
  62   const bool                _unloading_occurred;
  63   const uint                _num_workers;
  64 
  65   // Variables used to claim nmethods.
  66   CompiledMethod* _first_nmethod;
  67   CompiledMethod* volatile _claimed_nmethod;
  68 
  69 public:
  70   CodeCacheUnloadingTask(uint num_workers, BoolObjectClosure* is_alive, bool unloading_occurred);
  71   ~CodeCacheUnloadingTask();
  72 
  73 private:
  74   static const int MaxClaimNmethods = 16;
  75   void claim_nmethods(CompiledMethod** claimed_nmethods, int *num_claimed_nmethods);
  76 
  77 public:
  78   // Cleaning and unloading of nmethods.
  79   void work(uint worker_id);
  80 };
  81 
  82 
  83 class KlassCleaningTask : public StackObj {
  84   volatile int                            _clean_klass_tree_claimed;
  85   ClassLoaderDataGraphKlassIteratorAtomic _klass_iterator;
  86 
  87 public:
  88   KlassCleaningTask();
  89 
  90 private:
  91   bool claim_clean_klass_tree_task();
  92   InstanceKlass* claim_next_klass();
  93 
  94 public:
  95 
  96   void clean_klass(InstanceKlass* ik) {
  97     ik->clean_weak_instanceklass_links();
  98   }
  99 
 100   void work();
 101 };
 102 
 103 // To minimize the remark pause times, the tasks below are done in parallel.
 104 class ParallelCleaningTask : public AbstractGangTask {
 105 private:
 106   bool                        _unloading_occurred;
 107   StringCleaningTask          _string_task;
 108   CodeCacheUnloadingTask      _code_cache_task;
 109   KlassCleaningTask           _klass_cleaning_task;
 110 
 111 public:
 112   // The constructor is run in the VMThread.
 113   ParallelCleaningTask(BoolObjectClosure* is_alive, StringDedupUnlinkOrOopsDoClosure* dedup_closure,
 114     uint num_workers, bool unloading_occurred);
 115 
 116   void work(uint worker_id);
 117 };
 118 
 119 #endif // SHARE_VM_GC_SHARED_PARALLELCLEANING_HPP