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/stringTable.hpp"
  27 #include "classfile/systemDictionary.hpp"
  28 #include "gc/shared/weakProcessorPhases.hpp"
  29 #include "prims/resolvedMethodTable.hpp"
  30 #include "runtime/continuation.hpp"
  31 #include "runtime/jniHandles.hpp"
  32 #include "utilities/debug.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 #if INCLUDE_JFR
  36 #include "jfr/jfr.hpp"
  37 #endif // INCLUDE_JFR
  38 
  39 #if INCLUDE_JVMTI
  40 #include "prims/jvmtiExport.hpp"
  41 #endif // INCLUDE_JVMTI
  42 
  43 WeakProcessorPhases::Phase WeakProcessorPhases::phase(uint value) {
  44   assert(value < phase_count, "Invalid phase value %u", value);
  45   return static_cast<Phase>(value);
  46 }
  47 
  48 uint WeakProcessorPhases::index(Phase phase) {
  49   uint value = static_cast<uint>(phase);
  50   assert(value < phase_count, "Invalid phase %u", value);
  51   return value;
  52 }
  53 
  54 uint WeakProcessorPhases::serial_index(Phase phase) {
  55   assert(is_serial(phase), "not serial phase %u", index(phase));
  56   return index(phase) - serial_phase_start;
  57 }
  58 
  59 uint WeakProcessorPhases::oop_storage_index(Phase phase) {
  60   assert(is_oop_storage(phase), "not oop storage phase %u", index(phase));
  61   return index(phase) - oop_storage_phase_start;
  62 }
  63 
  64 bool WeakProcessorPhases::is_serial(Phase phase) {
  65   // serial_phase_count is 0 if JFR and JVMTI are both not built,
  66   // making this check with unsigned lhs redundant
  67 #if INCLUDE_JVMTI || INCLUDE_JFR
  68   return (index(phase) - serial_phase_start) < serial_phase_count;
  69 #else
  70   STATIC_ASSERT(serial_phase_count == 0);
  71   return false;
  72 #endif
  73 }
  74 
  75 bool WeakProcessorPhases::is_oop_storage(Phase phase) {
  76   return (index(phase) - oop_storage_phase_start) < oop_storage_phase_count;
  77 }
  78 
  79 const char* WeakProcessorPhases::description(Phase phase) {
  80   switch (phase) {
  81   JVMTI_ONLY(case jvmti: return "JVMTI weak processing";)
  82   JFR_ONLY(case jfr: return "JFR weak processing";)
  83   case jni: return "JNI weak processing";
  84   case stringtable: return "StringTable weak processing";
  85   case resolved_method_table: return "ResolvedMethodTable weak processing";
  86   case vm: return "VM weak processing";
  87   case nmethod_keepalive: return "NMethod keepalive (continuations)";
  88   default:
  89     ShouldNotReachHere();
  90     return "Invalid weak processing phase";
  91   }
  92 }
  93 
  94 WeakProcessorPhases::Processor WeakProcessorPhases::processor(Phase phase) {
  95   switch (phase) {
  96   JVMTI_ONLY(case jvmti: return &JvmtiExport::weak_oops_do;)
  97   JFR_ONLY(case jfr: return &Jfr::weak_oops_do;)
  98   default:
  99     ShouldNotReachHere();
 100     return NULL;
 101   }
 102 }
 103 
 104 OopStorage* WeakProcessorPhases::oop_storage(Phase phase) {
 105   switch (phase) {
 106   case jni: return JNIHandles::weak_global_handles();
 107   case stringtable: return StringTable::weak_storage();
 108   case resolved_method_table: return ResolvedMethodTable::weak_storage();
 109   case vm: return SystemDictionary::vm_weak_oop_storage();
 110   case nmethod_keepalive: return Continuation::weak_storage();
 111   default:
 112     ShouldNotReachHere();
 113     return NULL;
 114   }
 115 }
 116 
 117 bool WeakProcessorPhases::is_stringtable(Phase phase) {
 118   return phase == stringtable;
 119 }
 120 
 121 bool WeakProcessorPhases::is_resolved_method_table(Phase phase) {
 122   return phase == resolved_method_table;
 123 }