1 /*
   2  * Copyright (c) 2019, 2020, Red Hat, Inc. 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_GC_SHENANDOAH_SHENANDOAHNMETHOD_INLINE_HPP
  26 #define SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_INLINE_HPP
  27 
  28 #include "gc/shared/barrierSet.hpp"
  29 #include "gc/shared/barrierSetNMethod.hpp"
  30 #include "gc/shenandoah/shenandoahConcurrentRoots.hpp"
  31 #include "gc/shenandoah/shenandoahNMethod.hpp"
  32 
  33 nmethod* ShenandoahNMethod::nm() const {
  34   return _nm;
  35 }
  36 
  37 ShenandoahReentrantLock* ShenandoahNMethod::lock() {
  38   return &_lock;
  39 }
  40 
  41 int ShenandoahNMethod::oop_count() const {
  42   return _oops_count + static_cast<int>(nm()->oops_end() - nm()->oops_begin());
  43 }
  44 
  45 bool ShenandoahNMethod::has_oops() const {
  46   return oop_count() > 0;
  47 }
  48 
  49 void ShenandoahNMethod::mark_unregistered() {
  50   _unregistered = true;
  51 }
  52 
  53 bool ShenandoahNMethod::is_unregistered() const {
  54   return _unregistered;
  55 }
  56 
  57 void ShenandoahNMethod::oops_do(OopClosure* oops, bool fix_relocations) {
  58   for (int c = 0; c < _oops_count; c ++) {
  59     oops->do_oop(_oops[c]);
  60   }
  61 
  62   oop* const begin = _nm->oops_begin();
  63   oop* const end = _nm->oops_end();
  64   for (oop* p = begin; p < end; p++) {
  65     if (*p != Universe::non_oop_word()) {
  66       oops->do_oop(p);
  67     }
  68   }
  69 
  70   if (fix_relocations && _has_non_immed_oops) {
  71     _nm->fix_oop_relocations();
  72   }
  73 }
  74 
  75 void ShenandoahNMethod::heal_nmethod_metadata(ShenandoahNMethod* nmethod_data) {
  76   ShenandoahEvacuateUpdateRootsClosure<> cl;
  77   nmethod_data->oops_do(&cl, true /*fix relocation*/);
  78 }
  79 
  80 void ShenandoahNMethod::disarm_nmethod(nmethod* nm) {
  81   if (!ShenandoahConcurrentRoots::can_do_concurrent_class_unloading()) {
  82     return;
  83   }
  84 
  85   BarrierSetNMethod* const bs = BarrierSet::barrier_set()->barrier_set_nmethod();
  86   assert(bs != NULL, "Sanity");
  87   if (bs->is_armed(nm)) {
  88     bs->disarm(nm);
  89   }
  90 }
  91 
  92 ShenandoahNMethod* ShenandoahNMethod::gc_data(nmethod* nm) {
  93   return nm->gc_data<ShenandoahNMethod>();
  94 }
  95 
  96 void ShenandoahNMethod::attach_gc_data(nmethod* nm, ShenandoahNMethod* gc_data) {
  97   nm->set_gc_data<ShenandoahNMethod>(gc_data);
  98 }
  99 
 100 ShenandoahReentrantLock* ShenandoahNMethod::lock_for_nmethod(nmethod* nm) {
 101   return gc_data(nm)->lock();
 102 }
 103 
 104 bool ShenandoahNMethodTable::iteration_in_progress() const {
 105   shenandoah_assert_locked_or_safepoint(CodeCache_lock);
 106   return _itr_cnt > 0;
 107 }
 108 
 109 int ShenandoahNMethodList::size() const {
 110   return _size;
 111 }
 112 
 113 ShenandoahNMethod* ShenandoahNMethodList::at(int index) const {
 114   assert(index < size(), "Index out of bound");
 115   return _list[index];
 116 }
 117 
 118 void ShenandoahNMethodList::set(int index, ShenandoahNMethod* snm) {
 119   assert(index < size(), "Index out of bound");
 120   _list[index] = snm;
 121 }
 122 
 123 ShenandoahNMethod** ShenandoahNMethodList::list() const {
 124   return _list;
 125 }
 126 
 127 template<bool CSET_FILTER>
 128 void ShenandoahNMethodTableSnapshot::parallel_blobs_do(CodeBlobClosure *f) {
 129   size_t stride = 256; // educated guess
 130 
 131   ShenandoahNMethod** const list = _list->list();
 132 
 133   size_t max = (size_t)_limit;
 134   while (_claimed < max) {
 135     size_t cur = Atomic::fetch_and_add(&_claimed, stride);
 136     size_t start = cur;
 137     size_t end = MIN2(cur + stride, max);
 138     if (start >= max) break;
 139 
 140     for (size_t idx = start; idx < end; idx++) {
 141       ShenandoahNMethod* nmr = list[idx];
 142       assert(nmr != NULL, "Sanity");
 143       if (nmr->is_unregistered()) {
 144         continue;
 145       }
 146 
 147       nmr->assert_alive_and_correct();
 148 
 149       if (CSET_FILTER && !nmr->has_cset_oops(_heap)) {
 150         continue;
 151       }
 152 
 153       f->do_code_blob(nmr->nm());
 154     }
 155   }
 156 }
 157 
 158 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_INLINE_HPP