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 "gc/shared/oopStorageSet.hpp"
 27 #include "gc/shared/weakProcessorPhases.hpp"
 28 #include "utilities/debug.hpp"
 29 #include "utilities/macros.hpp"
 30 
 31 #if INCLUDE_JFR
 32 #include "jfr/jfr.hpp"
 33 #endif // INCLUDE_JFR
 34 
 35 #if INCLUDE_JVMTI
 36 #include "prims/jvmtiExport.hpp"
 37 #endif // INCLUDE_JVMTI
 38 
 39 #if INCLUDE_TSAN
 40 #include "tsan/tsanOopMap.hpp"
 41 #endif // INCLUDE_TSAN
 42 
 43 // serial_phase_count is 0 if JFR and JVMTI are both not built,
 44 // requiring some code to be careful to avoid tautological checks
 45 // that some compilers warn about.
 46 
 47 #define HAVE_SERIAL_PHASES (INCLUDE_JVMTI || INCLUDE_JFR || INCLUDE_TSAN)
 48 
 49 WeakProcessorPhases::Phase WeakProcessorPhases::serial_phase(uint value) {
 50 #if HAVE_SERIAL_PHASES
 51   assert(value < serial_phase_count, "Invalid serial phase value %u", value);
 52   return static_cast<Phase>(value + serial_phase_start);
 53 #else
 54   STATIC_ASSERT(serial_phase_count == 0);
 55   fatal("invalid serial phase value %u", value);
 56   return static_cast<Phase>(serial_phase_start);
 57 #endif // HAVE_SERIAL_PHASES
 58 }
 59 
 60 WeakProcessorPhases::Phase WeakProcessorPhases::oopstorage_phase(uint value) {
 61   assert(value < oopstorage_phase_count, "Invalid oopstorage phase value %u", value);
 62   return static_cast<Phase>(value + oopstorage_phase_start);
 63 }
 64 
 65 static uint raw_phase_index(WeakProcessorPhases::Phase phase) {
 66   return static_cast<uint>(phase);
 67 }
 68 
 69 uint WeakProcessorPhases::serial_index(Phase phase) {
 70   assert(is_serial(phase), "not serial phase %u", raw_phase_index(phase));
 71   return raw_phase_index(phase) - serial_phase_start;
 72 }
 73 
 74 uint WeakProcessorPhases::oopstorage_index(Phase phase) {
 75   assert(is_oopstorage(phase), "not oopstorage phase %u", raw_phase_index(phase));
 76   return raw_phase_index(phase) - oopstorage_phase_start;
 77 }
 78 
 79 static bool is_phase(WeakProcessorPhases::Phase phase, uint start, uint count) {
 80   return (raw_phase_index(phase) - start) < count;
 81 }
 82 
 83 bool WeakProcessorPhases::is_serial(Phase phase) {
 84 #if HAVE_SERIAL_PHASES
 85   return is_phase(phase, serial_phase_start, serial_phase_count);
 86 #else
 87   STATIC_ASSERT(serial_phase_count == 0);
 88   return false;
 89 #endif // HAVE_SERIAL_PHASES
 90 }
 91 
 92 bool WeakProcessorPhases::is_oopstorage(Phase phase) {
 93   return is_phase(phase, oopstorage_phase_start, oopstorage_phase_count);
 94 }
 95 
 96 #ifdef ASSERT
 97 
 98 void WeakProcessorPhases::Iterator::verify_nonsingular() const {
 99   assert(_limit != singular_value, "precondition");
100 }
101 
102 void WeakProcessorPhases::Iterator::verify_category_match(const Iterator& other) const {
103   verify_nonsingular();
104   assert(_limit == other._limit, "precondition");
105 }
106 
107 void WeakProcessorPhases::Iterator::verify_dereferenceable() const {
108   verify_nonsingular();
109   assert(_index < _limit, "precondition");
110 }
111 
112 #endif // ASSERT
113 
114 const char* WeakProcessorPhases::description(Phase phase) {
115   switch (phase) {
116   JVMTI_ONLY(case jvmti: return "JVMTI weak processing";)
117   JFR_ONLY(case jfr: return "JFR weak processing";)
118   TSAN_ONLY(case tsan: return "TSAN weak processing";)
119   default:
120     ShouldNotReachHere();
121     return "Invalid serial weak processing phase";
122   }
123 }
124 
125 WeakProcessorPhases::Processor WeakProcessorPhases::processor(Phase phase) {
126   switch (phase) {
127   JVMTI_ONLY(case jvmti: return &JvmtiExport::weak_oops_do;)
128   JFR_ONLY(case jfr: return &Jfr::weak_oops_do;)
129   TSAN_ONLY(case tsan: return &TsanOopMap::weak_oops_do;)
130   default:
131     ShouldNotReachHere();
132     return NULL;
133   }
134 }