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::disarm_nmethod(nmethod* nm) {
  58   if (!ShenandoahConcurrentRoots::can_do_concurrent_class_unloading()) {
  59     return;
  60   }
  61 
  62   BarrierSetNMethod* const bs = BarrierSet::barrier_set()->barrier_set_nmethod();
  63   assert(bs != NULL, "Sanity");
  64   if (bs->is_armed(nm)) {
  65     bs->disarm(nm);
  66   }
  67 }
  68 
  69 ShenandoahNMethod* ShenandoahNMethod::gc_data(nmethod* nm) {
  70   return nm->gc_data<ShenandoahNMethod>();
  71 }
  72 
  73 void ShenandoahNMethod::attach_gc_data(nmethod* nm, ShenandoahNMethod* gc_data) {
  74   nm->set_gc_data<ShenandoahNMethod>(gc_data);
  75 }
  76 
  77 ShenandoahReentrantLock* ShenandoahNMethod::lock_for_nmethod(nmethod* nm) {
  78   return gc_data(nm)->lock();
  79 }
  80 
  81 bool ShenandoahNMethodTable::iteration_in_progress() const {
  82   shenandoah_assert_locked_or_safepoint(CodeCache_lock);
  83   return _itr_cnt > 0;
  84 }
  85 
  86 int ShenandoahNMethodList::size() const {
  87   return _size;
  88 }
  89 
  90 ShenandoahNMethod* ShenandoahNMethodList::at(int index) const {
  91   assert(index < size(), "Index out of bound");
  92   return _list[index];
  93 }
  94 
  95 void ShenandoahNMethodList::set(int index, ShenandoahNMethod* snm) {
  96   assert(index < size(), "Index out of bound");
  97   _list[index] = snm;
  98 }
  99 
 100 ShenandoahNMethod** ShenandoahNMethodList::list() const {
 101   return _list;
 102 }
 103 
 104 template<bool CSET_FILTER>
 105 void ShenandoahNMethodTableSnapshot::parallel_blobs_do(CodeBlobClosure *f) {
 106   size_t stride = 256; // educated guess
 107 
 108   ShenandoahNMethod** const list = _list->list();
 109 
 110   size_t max = (size_t)_limit;
 111   while (_claimed < max) {
 112     size_t cur = Atomic::fetch_and_add(&_claimed, stride);
 113     size_t start = cur;
 114     size_t end = MIN2(cur + stride, max);
 115     if (start >= max) break;
 116 
 117     for (size_t idx = start; idx < end; idx++) {
 118       ShenandoahNMethod* nmr = list[idx];
 119       assert(nmr != NULL, "Sanity");
 120       if (nmr->is_unregistered()) {
 121         continue;
 122       }
 123 
 124       nmr->assert_alive_and_correct();
 125 
 126       if (CSET_FILTER && !nmr->has_cset_oops(_heap)) {
 127         continue;
 128       }
 129 
 130       f->do_code_blob(nmr->nm());
 131     }
 132   }
 133 }
 134 
 135 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHNMETHOD_INLINE_HPP