1 /*
   2  * Copyright (c) 2018, 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/symbolTable.hpp"
  27 #include "classfile/stringTable.hpp"
  28 #include "code/codeCache.hpp"
  29 #include "gc/shared/parallelCleaning.hpp"
  30 #include "logging/log.hpp"
  31 #include "memory/resourceArea.hpp"
  32 #include "logging/log.hpp"
  33 
  34 StringDedupCleaningTask::StringDedupCleaningTask(BoolObjectClosure* is_alive,
  35                                                  OopClosure* keep_alive,
  36                                                  bool resize_table) :
  37   AbstractGangTask("String Dedup Cleaning"),
  38   _dedup_closure(is_alive, keep_alive) {
  39 
  40   if (StringDedup::is_enabled()) {
  41     StringDedup::gc_prologue(resize_table);
  42   }
  43 }
  44 
  45 StringDedupCleaningTask::~StringDedupCleaningTask() {
  46   if (StringDedup::is_enabled()) {
  47     StringDedup::gc_epilogue();
  48   }
  49 }
  50 
  51 void StringDedupCleaningTask::work(uint worker_id) {
  52   if (StringDedup::is_enabled()) {
  53     StringDedup::parallel_unlink(&_dedup_closure, worker_id);
  54   }
  55 }
  56 
  57 CodeCacheUnloadingTask::CodeCacheUnloadingTask(uint num_workers, BoolObjectClosure* is_alive, bool unloading_occurred) :
  58   _unloading_scope(is_alive),
  59   _unloading_occurred(unloading_occurred),
  60   _num_workers(num_workers),
  61   _first_nmethod(NULL),
  62   _claimed_nmethod(NULL) {
  63   // Get first alive nmethod
  64   CompiledMethodIterator iter(CompiledMethodIterator::only_alive);
  65   if(iter.next()) {
  66     _first_nmethod = iter.method();
  67   }
  68   _claimed_nmethod = _first_nmethod;
  69 }
  70 
  71 CodeCacheUnloadingTask::~CodeCacheUnloadingTask() {
  72   CodeCache::verify_clean_inline_caches();
  73 
  74   guarantee(CodeCache::scavenge_root_nmethods() == NULL, "Must be");
  75 
  76   CodeCache::verify_icholder_relocations();
  77 }
  78 
  79 void CodeCacheUnloadingTask::claim_nmethods(CompiledMethod** claimed_nmethods, int *num_claimed_nmethods) {
  80   CompiledMethod* first;
  81   CompiledMethodIterator last(CompiledMethodIterator::only_alive);
  82 
  83   do {
  84     *num_claimed_nmethods = 0;
  85 
  86     first = _claimed_nmethod;
  87     last = CompiledMethodIterator(CompiledMethodIterator::only_alive, first);
  88 
  89     if (first != NULL) {
  90 
  91       for (int i = 0; i < MaxClaimNmethods; i++) {
  92         if (!last.next()) {
  93           break;
  94         }
  95         claimed_nmethods[i] = last.method();
  96         (*num_claimed_nmethods)++;
  97       }
  98     }
  99 
 100   } while (Atomic::cmpxchg(last.method(), &_claimed_nmethod, first) != first);
 101 }
 102 
 103 void CodeCacheUnloadingTask::work(uint worker_id) {
 104   // The first nmethods is claimed by the first worker.
 105   if (worker_id == 0 && _first_nmethod != NULL) {
 106     _first_nmethod->do_unloading(_unloading_occurred);
 107     _first_nmethod = NULL;
 108   }
 109 
 110   int num_claimed_nmethods;
 111   CompiledMethod* claimed_nmethods[MaxClaimNmethods];
 112 
 113   while (true) {
 114     claim_nmethods(claimed_nmethods, &num_claimed_nmethods);
 115 
 116     if (num_claimed_nmethods == 0) {
 117       break;
 118     }
 119 
 120     for (int i = 0; i < num_claimed_nmethods; i++) {
 121       claimed_nmethods[i]->do_unloading(_unloading_occurred);
 122     }
 123   }
 124 }
 125 
 126 KlassCleaningTask::KlassCleaningTask() :
 127   _clean_klass_tree_claimed(0),
 128   _klass_iterator() {
 129 }
 130 
 131 bool KlassCleaningTask::claim_clean_klass_tree_task() {
 132   if (_clean_klass_tree_claimed) {
 133     return false;
 134   }
 135 
 136   return Atomic::cmpxchg(1, &_clean_klass_tree_claimed, 0) == 0;
 137 }
 138 
 139 InstanceKlass* KlassCleaningTask::claim_next_klass() {
 140   Klass* klass;
 141   do {
 142     klass =_klass_iterator.next_klass();
 143   } while (klass != NULL && !klass->is_instance_klass());
 144 
 145   // this can be null so don't call InstanceKlass::cast
 146   return static_cast<InstanceKlass*>(klass);
 147 }
 148 
 149 void KlassCleaningTask::work() {
 150   ResourceMark rm;
 151 
 152   // One worker will clean the subklass/sibling klass tree.
 153   if (claim_clean_klass_tree_task()) {
 154     Klass::clean_subklass_tree();
 155   }
 156 
 157   // All workers will help cleaning the classes,
 158   InstanceKlass* klass;
 159   while ((klass = claim_next_klass()) != NULL) {
 160     clean_klass(klass);
 161   }
 162 }
 163 
 164 ParallelCleaningTask::ParallelCleaningTask(BoolObjectClosure* is_alive,
 165                                            uint num_workers,
 166                                            bool unloading_occurred,
 167                                            bool resize_dedup_table) :
 168   AbstractGangTask("Parallel Cleaning"),
 169   _unloading_occurred(unloading_occurred),
 170   _string_dedup_task(is_alive, NULL, resize_dedup_table),
 171   _code_cache_task(num_workers, is_alive, unloading_occurred),
 172   _klass_cleaning_task() {
 173 }
 174 
 175 // The parallel work done by all worker threads.
 176 void ParallelCleaningTask::work(uint worker_id) {
 177   // Do first pass of code cache cleaning.
 178   _code_cache_task.work(worker_id);
 179 
 180   // Clean the string dedup data structures.
 181   _string_dedup_task.work(worker_id);
 182 
 183   // Clean all klasses that were not unloaded.
 184   // The weak metadata in klass doesn't need to be
 185   // processed if there was no unloading.
 186   if (_unloading_occurred) {
 187     _klass_cleaning_task.work();
 188   }
 189 }