author | egahlin |
Fri, 17 May 2019 16:02:27 +0200 | |
branch | JEP-349-branch |
changeset 57360 | 5d043a159d5c |
parent 54623 | 1126f0607c70 |
child 57870 | 00860d9caf4d |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
52212
diff
changeset
|
2 |
* Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved. |
50113 | 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" |
|
57360 | 26 |
#include "jfr/jfr.hpp" |
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
27 |
#include "jfr/metadata/jfrSerializer.hpp" |
50113 | 28 |
#include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp" |
29 |
#include "jfr/recorder/checkpoint/types/jfrType.hpp" |
|
30 |
#include "jfr/recorder/checkpoint/types/jfrTypeManager.hpp" |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
31 |
#include "jfr/utilities/jfrDoublyLinkedList.hpp" |
50113 | 32 |
#include "jfr/utilities/jfrIterator.hpp" |
33 |
#include "runtime/safepoint.hpp" |
|
34 |
#include "runtime/thread.inline.hpp" |
|
35 |
#include "utilities/exceptions.hpp" |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
36 |
#include "runtime/semaphore.hpp" |
50113 | 37 |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
38 |
class JfrSerializerRegistration : public JfrCHeapObj { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
39 |
private: |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
40 |
JfrSerializerRegistration* _next; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
41 |
JfrSerializerRegistration* _prev; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
42 |
JfrSerializer* _serializer; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
43 |
mutable JfrCheckpointBlobHandle _cache; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
44 |
JfrTypeId _id; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
45 |
bool _permit_cache; |
50113 | 46 |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
47 |
public: |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
48 |
JfrSerializerRegistration(JfrTypeId id, bool permit_cache, JfrSerializer* serializer) : |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
49 |
_next(NULL), _prev(NULL), _serializer(serializer), _cache(), _id(id), _permit_cache(permit_cache) {} |
50113 | 50 |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
51 |
~JfrSerializerRegistration() { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
52 |
delete _serializer; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
53 |
} |
50113 | 54 |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
55 |
JfrSerializerRegistration* next() const { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
56 |
return _next; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
57 |
} |
50113 | 58 |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
59 |
void set_next(JfrSerializerRegistration* next) { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
60 |
_next = next; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
61 |
} |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
62 |
|
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
63 |
JfrSerializerRegistration* prev() const { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
64 |
return _prev; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
65 |
} |
50113 | 66 |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
67 |
void set_prev(JfrSerializerRegistration* prev) { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
68 |
_prev = prev; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
69 |
} |
50113 | 70 |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
71 |
JfrTypeId id() const { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
72 |
return _id; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
73 |
} |
50113 | 74 |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
75 |
void invoke(JfrCheckpointWriter& writer) const; |
57360 | 76 |
|
77 |
void on_rotation() const; |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
78 |
}; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
79 |
|
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
80 |
void JfrSerializerRegistration::invoke(JfrCheckpointWriter& writer) const { |
50113 | 81 |
if (_cache.valid()) { |
82 |
writer.increment(); |
|
83 |
_cache->write(writer); |
|
84 |
return; |
|
85 |
} |
|
86 |
const JfrCheckpointContext ctx = writer.context(); |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
87 |
// serialize the type id before invoking callback |
50113 | 88 |
writer.write_type(_id); |
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
89 |
const intptr_t start = writer.current_offset(); |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
90 |
// invoke the serializer routine |
50113 | 91 |
_serializer->serialize(writer); |
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
92 |
if (start == writer.current_offset() ) { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
93 |
// the serializer implementation did nothing, rewind to restore |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
94 |
writer.set_context(ctx); |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
95 |
return; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
96 |
} |
50113 | 97 |
if (_permit_cache) { |
98 |
_cache = writer.copy(&ctx); |
|
99 |
} |
|
100 |
} |
|
101 |
||
57360 | 102 |
void JfrSerializerRegistration::on_rotation() const { |
103 |
_serializer->on_rotation(); |
|
104 |
} |
|
105 |
||
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
106 |
class SerializerRegistrationGuard : public StackObj { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
107 |
private: |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
108 |
static Semaphore _mutex_semaphore; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
109 |
public: |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
110 |
SerializerRegistrationGuard() { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
111 |
_mutex_semaphore.wait(); |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
112 |
} |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
113 |
~SerializerRegistrationGuard() { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
114 |
_mutex_semaphore.signal(); |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
115 |
} |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
116 |
}; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
117 |
|
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
118 |
Semaphore SerializerRegistrationGuard::_mutex_semaphore(1); |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
119 |
|
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
120 |
typedef JfrDoublyLinkedList<JfrSerializerRegistration> List; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
121 |
typedef StopOnNullIterator<const List> Iterator; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
122 |
static List types; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
123 |
|
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
124 |
void JfrTypeManager::clear() { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
125 |
SerializerRegistrationGuard guard; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
126 |
Iterator iter(types); |
50113 | 127 |
JfrSerializerRegistration* registration; |
128 |
while (iter.has_next()) { |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
129 |
registration = types.remove(iter.next()); |
50113 | 130 |
assert(registration != NULL, "invariant"); |
131 |
delete registration; |
|
132 |
} |
|
133 |
} |
|
134 |
||
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
135 |
void JfrTypeManager::write_types(JfrCheckpointWriter& writer) { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
136 |
const Iterator iter(types); |
50113 | 137 |
while (iter.has_next()) { |
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
138 |
iter.next()->invoke(writer); |
50113 | 139 |
} |
140 |
} |
|
141 |
||
57360 | 142 |
void JfrTypeManager::notify_types_on_rotation() { |
143 |
const Iterator iter(types); |
|
50113 | 144 |
while (iter.has_next()) { |
57360 | 145 |
iter.next()->on_rotation(); |
50113 | 146 |
} |
147 |
} |
|
148 |
||
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
149 |
void JfrTypeManager::write_type_set() { |
50113 | 150 |
// can safepoint here because of Module_lock |
57360 | 151 |
assert(!SafepointSynchronize::is_at_safepoint(), "invariant"); |
50113 | 152 |
JfrCheckpointWriter writer(true, true, Thread::current()); |
153 |
TypeSet set; |
|
154 |
set.serialize(writer); |
|
155 |
} |
|
156 |
||
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
157 |
void JfrTypeManager::write_type_set_for_unloaded_classes() { |
57360 | 158 |
assert(SafepointSynchronize::is_at_safepoint(), "invariant"); |
50113 | 159 |
JfrCheckpointWriter writer(false, true, Thread::current()); |
160 |
ClassUnloadTypeSet class_unload_set; |
|
161 |
class_unload_set.serialize(writer); |
|
162 |
} |
|
163 |
||
57360 | 164 |
size_t JfrTypeManager::flush_type_set() { |
165 |
assert(!SafepointSynchronize::is_at_safepoint(), "invariant"); |
|
166 |
JfrCheckpointWriter writer(true, true, Thread::current()); |
|
167 |
FlushTypeSet flush; |
|
168 |
flush.serialize(writer); |
|
169 |
return flush.elements(); |
|
170 |
} |
|
171 |
||
172 |
void JfrTypeManager::create_thread_checkpoint(Thread* t) { |
|
173 |
assert(t != NULL, "invariant"); |
|
174 |
JfrThreadConstant type_thread(t); |
|
175 |
JfrCheckpointWriter writer(false, true, t); |
|
50113 | 176 |
writer.write_type(TYPE_THREAD); |
177 |
type_thread.serialize(writer); |
|
178 |
// create and install a checkpoint blob |
|
57360 | 179 |
t->jfr_thread_local()->set_thread_checkpoint(writer.checkpoint_blob()); |
180 |
assert(t->jfr_thread_local()->has_thread_checkpoint(), "invariant"); |
|
50113 | 181 |
} |
182 |
||
57360 | 183 |
void JfrTypeManager::write_thread_checkpoint(Thread* t) { |
184 |
assert(t != NULL, "invariant"); |
|
185 |
JfrThreadConstant type_thread(t); |
|
186 |
JfrCheckpointWriter writer(false, true, t); |
|
187 |
writer.write_type(TYPE_THREAD); |
|
188 |
type_thread.serialize(writer); |
|
50113 | 189 |
} |
190 |
||
191 |
#ifdef ASSERT |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
192 |
static void assert_not_registered_twice(JfrTypeId id, List& list) { |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
193 |
const Iterator iter(list); |
50113 | 194 |
while (iter.has_next()) { |
195 |
assert(iter.next()->id() != id, "invariant"); |
|
196 |
} |
|
197 |
} |
|
198 |
#endif |
|
199 |
||
57360 | 200 |
static bool register_type(JfrTypeId id, bool permit_cache, JfrSerializer* serializer) { |
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
201 |
assert(serializer != NULL, "invariant"); |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
202 |
JfrSerializerRegistration* const registration = new JfrSerializerRegistration(id, permit_cache, serializer); |
50113 | 203 |
if (registration == NULL) { |
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
204 |
delete serializer; |
50113 | 205 |
return false; |
206 |
} |
|
57360 | 207 |
|
208 |
assert(!types.in_list(registration), "invariant"); |
|
209 |
DEBUG_ONLY(assert_not_registered_twice(id, types);) |
|
210 |
if (Jfr::is_recording()) { |
|
211 |
JfrCheckpointWriter writer(false, true, Thread::current()); |
|
212 |
registration->invoke(writer); |
|
50113 | 213 |
} |
57360 | 214 |
types.prepend(registration); |
50113 | 215 |
return true; |
216 |
} |
|
217 |
||
218 |
bool JfrTypeManager::initialize() { |
|
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
219 |
SerializerRegistrationGuard guard; |
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
220 |
|
50113 | 221 |
// register non-safepointing type serialization |
57360 | 222 |
register_type(TYPE_THREADGROUP, false, new JfrThreadGroupConstant()); |
223 |
register_type(TYPE_THREAD, false, new JfrThreadConstantSet()); |
|
224 |
register_type(TYPE_FLAGVALUEORIGIN, true, new FlagValueOriginConstant()); |
|
225 |
register_type(TYPE_INFLATECAUSE, true, new MonitorInflateCauseConstant()); |
|
226 |
register_type(TYPE_GCCAUSE, true, new GCCauseConstant()); |
|
227 |
register_type(TYPE_GCNAME, true, new GCNameConstant()); |
|
228 |
register_type(TYPE_GCWHEN, true, new GCWhenConstant()); |
|
229 |
register_type(TYPE_G1HEAPREGIONTYPE, true, new G1HeapRegionTypeConstant()); |
|
230 |
register_type(TYPE_GCTHRESHOLDUPDATER, true, new GCThresholdUpdaterConstant()); |
|
231 |
register_type(TYPE_METADATATYPE, true, new MetadataTypeConstant()); |
|
232 |
register_type(TYPE_METASPACEOBJECTTYPE, true, new MetaspaceObjectTypeConstant()); |
|
233 |
register_type(TYPE_G1YCTYPE, true, new G1YCTypeConstant()); |
|
234 |
register_type(TYPE_REFERENCETYPE, true, new ReferenceTypeConstant()); |
|
235 |
register_type(TYPE_NARROWOOPMODE, true, new NarrowOopModeConstant()); |
|
236 |
register_type(TYPE_COMPILERPHASETYPE, true, new CompilerPhaseTypeConstant()); |
|
237 |
register_type(TYPE_CODEBLOBTYPE, true, new CodeBlobTypeConstant()); |
|
238 |
register_type(TYPE_VMOPERATIONTYPE, true, new VMOperationTypeConstant()); |
|
239 |
register_type(TYPE_THREADSTATE, true, new ThreadStateConstant()); |
|
50113 | 240 |
|
241 |
return true; |
|
242 |
} |
|
243 |
||
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
244 |
// implementation for the static registration function exposed in the JfrSerializer api |
57360 | 245 |
bool JfrSerializer::register_serializer(JfrTypeId id, bool permit_cache, JfrSerializer* serializer) { |
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
246 |
SerializerRegistrationGuard guard; |
57360 | 247 |
return register_type(id, permit_cache, serializer); |
50703
0486d7749b8d
8205053: Decouple type registration from framework lifecycle
egahlin
parents:
50525
diff
changeset
|
248 |
} |