Mon, 19 Aug 2019 12:47:38 +0200
8229873: 8229401 broke jdk8u-jfr-incubator
Reviewed-by: neugens
apetushkov@9858 | 1 | /* |
apetushkov@9858 | 2 | * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved. |
apetushkov@9858 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
apetushkov@9858 | 4 | * |
apetushkov@9858 | 5 | * This code is free software; you can redistribute it and/or modify it |
apetushkov@9858 | 6 | * under the terms of the GNU General Public License version 2 only, as |
apetushkov@9858 | 7 | * published by the Free Software Foundation. |
apetushkov@9858 | 8 | * |
apetushkov@9858 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
apetushkov@9858 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
apetushkov@9858 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
apetushkov@9858 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
apetushkov@9858 | 13 | * accompanied this code). |
apetushkov@9858 | 14 | * |
apetushkov@9858 | 15 | * You should have received a copy of the GNU General Public License version |
apetushkov@9858 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
apetushkov@9858 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
apetushkov@9858 | 18 | * |
apetushkov@9858 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
apetushkov@9858 | 20 | * or visit www.oracle.com if you need additional information or have any |
apetushkov@9858 | 21 | * questions. |
apetushkov@9858 | 22 | * |
apetushkov@9858 | 23 | */ |
apetushkov@9858 | 24 | |
apetushkov@9858 | 25 | #include "precompiled.hpp" |
apetushkov@9858 | 26 | #include "jfr/metadata/jfrSerializer.hpp" |
apetushkov@9858 | 27 | #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp" |
apetushkov@9858 | 28 | #include "jfr/recorder/checkpoint/types/jfrType.hpp" |
apetushkov@9858 | 29 | #include "jfr/recorder/checkpoint/types/jfrTypeManager.hpp" |
apetushkov@9858 | 30 | #include "jfr/utilities/jfrDoublyLinkedList.hpp" |
apetushkov@9858 | 31 | #include "jfr/utilities/jfrIterator.hpp" |
apetushkov@9858 | 32 | #include "runtime/safepoint.hpp" |
apetushkov@9858 | 33 | #include "runtime/thread.inline.hpp" |
apetushkov@9858 | 34 | #include "utilities/exceptions.hpp" |
apetushkov@9858 | 35 | #include "runtime/semaphore.hpp" |
apetushkov@9858 | 36 | |
apetushkov@9858 | 37 | class JfrSerializerRegistration : public JfrCHeapObj { |
apetushkov@9858 | 38 | private: |
apetushkov@9858 | 39 | JfrSerializerRegistration* _next; |
apetushkov@9858 | 40 | JfrSerializerRegistration* _prev; |
apetushkov@9858 | 41 | JfrSerializer* _serializer; |
apetushkov@9858 | 42 | mutable JfrCheckpointBlobHandle _cache; |
apetushkov@9858 | 43 | JfrTypeId _id; |
apetushkov@9858 | 44 | bool _permit_cache; |
apetushkov@9858 | 45 | |
apetushkov@9858 | 46 | public: |
apetushkov@9858 | 47 | JfrSerializerRegistration(JfrTypeId id, bool permit_cache, JfrSerializer* serializer) : |
apetushkov@9858 | 48 | _next(NULL), _prev(NULL), _serializer(serializer), _cache(), _id(id), _permit_cache(permit_cache) {} |
apetushkov@9858 | 49 | |
apetushkov@9858 | 50 | ~JfrSerializerRegistration() { |
apetushkov@9858 | 51 | delete _serializer; |
apetushkov@9858 | 52 | } |
apetushkov@9858 | 53 | |
apetushkov@9858 | 54 | JfrSerializerRegistration* next() const { |
apetushkov@9858 | 55 | return _next; |
apetushkov@9858 | 56 | } |
apetushkov@9858 | 57 | |
apetushkov@9858 | 58 | void set_next(JfrSerializerRegistration* next) { |
apetushkov@9858 | 59 | _next = next; |
apetushkov@9858 | 60 | } |
apetushkov@9858 | 61 | |
apetushkov@9858 | 62 | JfrSerializerRegistration* prev() const { |
apetushkov@9858 | 63 | return _prev; |
apetushkov@9858 | 64 | } |
apetushkov@9858 | 65 | |
apetushkov@9858 | 66 | void set_prev(JfrSerializerRegistration* prev) { |
apetushkov@9858 | 67 | _prev = prev; |
apetushkov@9858 | 68 | } |
apetushkov@9858 | 69 | |
apetushkov@9858 | 70 | JfrTypeId id() const { |
apetushkov@9858 | 71 | return _id; |
apetushkov@9858 | 72 | } |
apetushkov@9858 | 73 | |
apetushkov@9858 | 74 | void invoke(JfrCheckpointWriter& writer) const; |
apetushkov@9858 | 75 | }; |
apetushkov@9858 | 76 | |
apetushkov@9858 | 77 | void JfrSerializerRegistration::invoke(JfrCheckpointWriter& writer) const { |
apetushkov@9858 | 78 | if (_cache.valid()) { |
apetushkov@9858 | 79 | writer.increment(); |
apetushkov@9858 | 80 | _cache->write(writer); |
apetushkov@9858 | 81 | return; |
apetushkov@9858 | 82 | } |
apetushkov@9858 | 83 | const JfrCheckpointContext ctx = writer.context(); |
apetushkov@9858 | 84 | // serialize the type id before invoking callback |
apetushkov@9858 | 85 | writer.write_type(_id); |
apetushkov@9858 | 86 | const intptr_t start = writer.current_offset(); |
apetushkov@9858 | 87 | // invoke the serializer routine |
apetushkov@9858 | 88 | _serializer->serialize(writer); |
apetushkov@9858 | 89 | if (start == writer.current_offset() ) { |
apetushkov@9858 | 90 | // the serializer implementation did nothing, rewind to restore |
apetushkov@9858 | 91 | writer.set_context(ctx); |
apetushkov@9858 | 92 | return; |
apetushkov@9858 | 93 | } |
apetushkov@9858 | 94 | if (_permit_cache) { |
apetushkov@9858 | 95 | _cache = writer.copy(&ctx); |
apetushkov@9858 | 96 | } |
apetushkov@9858 | 97 | } |
apetushkov@9858 | 98 | |
apetushkov@9858 | 99 | class SerializerRegistrationGuard : public StackObj { |
apetushkov@9858 | 100 | private: |
apetushkov@9858 | 101 | static Semaphore _mutex_semaphore; |
apetushkov@9858 | 102 | public: |
apetushkov@9858 | 103 | SerializerRegistrationGuard() { |
apetushkov@9858 | 104 | _mutex_semaphore.wait(); |
apetushkov@9858 | 105 | } |
apetushkov@9858 | 106 | ~SerializerRegistrationGuard() { |
apetushkov@9858 | 107 | _mutex_semaphore.signal(); |
apetushkov@9858 | 108 | } |
apetushkov@9858 | 109 | }; |
apetushkov@9858 | 110 | |
apetushkov@9858 | 111 | Semaphore SerializerRegistrationGuard::_mutex_semaphore(1); |
apetushkov@9858 | 112 | |
apetushkov@9858 | 113 | typedef JfrDoublyLinkedList<JfrSerializerRegistration> List; |
apetushkov@9858 | 114 | typedef StopOnNullIterator<const List> Iterator; |
apetushkov@9858 | 115 | static List types; |
apetushkov@9858 | 116 | static List safepoint_types; |
apetushkov@9858 | 117 | |
apetushkov@9858 | 118 | void JfrTypeManager::clear() { |
apetushkov@9858 | 119 | SerializerRegistrationGuard guard; |
apetushkov@9858 | 120 | Iterator iter(types); |
apetushkov@9858 | 121 | JfrSerializerRegistration* registration; |
apetushkov@9858 | 122 | while (iter.has_next()) { |
apetushkov@9858 | 123 | registration = types.remove(iter.next()); |
apetushkov@9858 | 124 | assert(registration != NULL, "invariant"); |
apetushkov@9858 | 125 | delete registration; |
apetushkov@9858 | 126 | } |
apetushkov@9858 | 127 | Iterator sp_type_iter(safepoint_types); |
apetushkov@9858 | 128 | while (sp_type_iter.has_next()) { |
apetushkov@9858 | 129 | registration = safepoint_types.remove(sp_type_iter.next()); |
apetushkov@9858 | 130 | assert(registration != NULL, "invariant"); |
apetushkov@9858 | 131 | delete registration; |
apetushkov@9858 | 132 | } |
apetushkov@9858 | 133 | } |
apetushkov@9858 | 134 | |
apetushkov@9858 | 135 | void JfrTypeManager::write_types(JfrCheckpointWriter& writer) { |
apetushkov@9858 | 136 | const Iterator iter(types); |
apetushkov@9858 | 137 | while (iter.has_next()) { |
apetushkov@9858 | 138 | iter.next()->invoke(writer); |
apetushkov@9858 | 139 | } |
apetushkov@9858 | 140 | } |
apetushkov@9858 | 141 | |
apetushkov@9858 | 142 | void JfrTypeManager::write_safepoint_types(JfrCheckpointWriter& writer) { |
apetushkov@9858 | 143 | assert(SafepointSynchronize::is_at_safepoint(), "invariant"); |
apetushkov@9858 | 144 | const Iterator iter(safepoint_types); |
apetushkov@9858 | 145 | while (iter.has_next()) { |
apetushkov@9858 | 146 | iter.next()->invoke(writer); |
apetushkov@9858 | 147 | } |
apetushkov@9858 | 148 | } |
apetushkov@9858 | 149 | |
apetushkov@9858 | 150 | void JfrTypeManager::write_type_set() { |
apetushkov@9858 | 151 | // can safepoint here because of PackageTable_lock |
apetushkov@9858 | 152 | MutexLockerEx lock(SafepointSynchronize::is_at_safepoint() ? NULL : PackageTable_lock); |
apetushkov@9858 | 153 | JfrCheckpointWriter writer(true, true, Thread::current()); |
apetushkov@9858 | 154 | TypeSet set; |
apetushkov@9858 | 155 | set.serialize(writer); |
apetushkov@9858 | 156 | } |
apetushkov@9858 | 157 | |
apetushkov@9858 | 158 | void JfrTypeManager::write_type_set_for_unloaded_classes() { |
apetushkov@9858 | 159 | assert(SafepointSynchronize::is_at_safepoint(), "invariant"); |
apetushkov@9858 | 160 | JfrCheckpointWriter writer(false, true, Thread::current()); |
apetushkov@9858 | 161 | ClassUnloadTypeSet class_unload_set; |
apetushkov@9858 | 162 | class_unload_set.serialize(writer); |
apetushkov@9858 | 163 | } |
apetushkov@9858 | 164 | |
apetushkov@9858 | 165 | void JfrTypeManager::create_thread_checkpoint(JavaThread* jt) { |
apetushkov@9858 | 166 | assert(jt != NULL, "invariant"); |
apetushkov@9858 | 167 | JfrThreadConstant type_thread(jt); |
apetushkov@9858 | 168 | JfrCheckpointWriter writer(false, true, jt); |
apetushkov@9858 | 169 | writer.write_type(TYPE_THREAD); |
apetushkov@9858 | 170 | type_thread.serialize(writer); |
apetushkov@9858 | 171 | // create and install a checkpoint blob |
apetushkov@9858 | 172 | jt->jfr_thread_local()->set_thread_checkpoint(writer.checkpoint_blob()); |
apetushkov@9858 | 173 | assert(jt->jfr_thread_local()->has_thread_checkpoint(), "invariant"); |
apetushkov@9858 | 174 | } |
apetushkov@9858 | 175 | |
apetushkov@9858 | 176 | void JfrTypeManager::write_thread_checkpoint(JavaThread* jt) { |
apetushkov@9858 | 177 | assert(jt != NULL, "JavaThread is NULL!"); |
apetushkov@9858 | 178 | ResourceMark rm(jt); |
apetushkov@9858 | 179 | if (jt->jfr_thread_local()->has_thread_checkpoint()) { |
apetushkov@9858 | 180 | JfrCheckpointWriter writer(false, false, jt); |
apetushkov@9858 | 181 | jt->jfr_thread_local()->thread_checkpoint()->write(writer); |
apetushkov@9858 | 182 | } else { |
apetushkov@9858 | 183 | JfrThreadConstant type_thread(jt); |
apetushkov@9858 | 184 | JfrCheckpointWriter writer(false, true, jt); |
apetushkov@9858 | 185 | writer.write_type(TYPE_THREAD); |
apetushkov@9858 | 186 | type_thread.serialize(writer); |
apetushkov@9858 | 187 | } |
apetushkov@9858 | 188 | } |
apetushkov@9858 | 189 | |
apetushkov@9858 | 190 | #ifdef ASSERT |
apetushkov@9858 | 191 | static void assert_not_registered_twice(JfrTypeId id, List& list) { |
apetushkov@9858 | 192 | const Iterator iter(list); |
apetushkov@9858 | 193 | while (iter.has_next()) { |
apetushkov@9858 | 194 | assert(iter.next()->id() != id, "invariant"); |
apetushkov@9858 | 195 | } |
apetushkov@9858 | 196 | } |
apetushkov@9858 | 197 | #endif |
apetushkov@9858 | 198 | |
apetushkov@9858 | 199 | static bool register_type(JfrTypeId id, bool require_safepoint, bool permit_cache, JfrSerializer* serializer) { |
apetushkov@9858 | 200 | assert(serializer != NULL, "invariant"); |
apetushkov@9858 | 201 | JfrSerializerRegistration* const registration = new JfrSerializerRegistration(id, permit_cache, serializer); |
apetushkov@9858 | 202 | if (registration == NULL) { |
apetushkov@9858 | 203 | delete serializer; |
apetushkov@9858 | 204 | return false; |
apetushkov@9858 | 205 | } |
apetushkov@9858 | 206 | if (require_safepoint) { |
apetushkov@9858 | 207 | assert(!safepoint_types.in_list(registration), "invariant"); |
apetushkov@9858 | 208 | DEBUG_ONLY(assert_not_registered_twice(id, safepoint_types);) |
apetushkov@9858 | 209 | safepoint_types.prepend(registration); |
apetushkov@9858 | 210 | } else { |
apetushkov@9858 | 211 | assert(!types.in_list(registration), "invariant"); |
apetushkov@9858 | 212 | DEBUG_ONLY(assert_not_registered_twice(id, types);) |
apetushkov@9858 | 213 | types.prepend(registration); |
apetushkov@9858 | 214 | } |
apetushkov@9858 | 215 | return true; |
apetushkov@9858 | 216 | } |
apetushkov@9858 | 217 | |
apetushkov@9858 | 218 | bool JfrTypeManager::initialize() { |
apetushkov@9858 | 219 | SerializerRegistrationGuard guard; |
apetushkov@9858 | 220 | |
apetushkov@9858 | 221 | // register non-safepointing type serialization |
apetushkov@9858 | 222 | register_type(TYPE_FLAGVALUEORIGIN, false, true, new FlagValueOriginConstant()); |
apetushkov@9858 | 223 | register_type(TYPE_INFLATECAUSE, false, true, new MonitorInflateCauseConstant()); |
apetushkov@9858 | 224 | register_type(TYPE_GCCAUSE, false, true, new GCCauseConstant()); |
apetushkov@9858 | 225 | register_type(TYPE_GCNAME, false, true, new GCNameConstant()); |
apetushkov@9858 | 226 | register_type(TYPE_GCWHEN, false, true, new GCWhenConstant()); |
apetushkov@9858 | 227 | register_type(TYPE_G1HEAPREGIONTYPE, false, true, new G1HeapRegionTypeConstant()); |
apetushkov@9858 | 228 | register_type(TYPE_GCTHRESHOLDUPDATER, false, true, new GCThresholdUpdaterConstant()); |
apetushkov@9858 | 229 | register_type(TYPE_METADATATYPE, false, true, new MetadataTypeConstant()); |
apetushkov@9858 | 230 | register_type(TYPE_METASPACEOBJECTTYPE, false, true, new MetaspaceObjectTypeConstant()); |
apetushkov@9858 | 231 | register_type(TYPE_G1YCTYPE, false, true, new G1YCTypeConstant()); |
apetushkov@9858 | 232 | register_type(TYPE_REFERENCETYPE, false, true, new ReferenceTypeConstant()); |
apetushkov@9858 | 233 | register_type(TYPE_NARROWOOPMODE, false, true, new NarrowOopModeConstant()); |
apetushkov@9858 | 234 | register_type(TYPE_COMPILERPHASETYPE, false, true, new CompilerPhaseTypeConstant()); |
apetushkov@9858 | 235 | register_type(TYPE_CODEBLOBTYPE, false, true, new CodeBlobTypeConstant()); |
apetushkov@9858 | 236 | register_type(TYPE_VMOPERATIONTYPE, false, true, new VMOperationTypeConstant()); |
apetushkov@9858 | 237 | register_type(TYPE_THREADSTATE, false, true, new ThreadStateConstant()); |
apetushkov@9858 | 238 | |
apetushkov@9858 | 239 | // register safepointing type serialization |
apetushkov@9858 | 240 | register_type(TYPE_THREADGROUP, true, false, new JfrThreadGroupConstant()); |
apetushkov@9858 | 241 | register_type(TYPE_THREAD, true, false, new JfrThreadConstantSet()); |
apetushkov@9858 | 242 | return true; |
apetushkov@9858 | 243 | } |
apetushkov@9858 | 244 | |
apetushkov@9858 | 245 | // implementation for the static registration function exposed in the JfrSerializer api |
apetushkov@9858 | 246 | bool JfrSerializer::register_serializer(JfrTypeId id, bool require_safepoint, bool permit_cache, JfrSerializer* serializer) { |
apetushkov@9858 | 247 | SerializerRegistrationGuard guard; |
apetushkov@9858 | 248 | return register_type(id, require_safepoint, permit_cache, serializer); |
apetushkov@9858 | 249 | } |