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