author | egahlin |
Fri, 17 May 2019 16:02:27 +0200 | |
branch | JEP-349-branch |
changeset 57360 | 5d043a159d5c |
parent 53244 | 9807daeb47c4 |
child 57870 | 00860d9caf4d |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
51444
diff
changeset
|
2 |
* Copyright (c) 2017, 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 |
||
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
51444
diff
changeset
|
25 |
#ifndef SHARE_JFR_RECORDER_CHECKPOINT_TYPES_JFRTYPESETUTILS_HPP |
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
51444
diff
changeset
|
26 |
#define SHARE_JFR_RECORDER_CHECKPOINT_TYPES_JFRTYPESETUTILS_HPP |
50113 | 27 |
|
28 |
#include "jfr/recorder/checkpoint/types/traceid/jfrTraceId.inline.hpp" |
|
29 |
#include "jfr/utilities/jfrAllocation.hpp" |
|
30 |
#include "jfr/utilities/jfrHashtable.hpp" |
|
31 |
#include "oops/klass.hpp" |
|
32 |
#include "oops/method.hpp" |
|
33 |
#include "utilities/growableArray.hpp" |
|
34 |
||
35 |
// Composite callback/functor building block |
|
36 |
template <typename T, typename Func1, typename Func2> |
|
37 |
class CompositeFunctor { |
|
38 |
private: |
|
39 |
Func1* _f; |
|
40 |
Func2* _g; |
|
41 |
public: |
|
42 |
CompositeFunctor(Func1* f, Func2* g) : _f(f), _g(g) { |
|
43 |
assert(f != NULL, "invariant"); |
|
44 |
assert(g != NULL, "invariant"); |
|
45 |
} |
|
46 |
bool operator()(T const& value) { |
|
47 |
return (*_f)(value) && (*_g)(value); |
|
48 |
} |
|
49 |
}; |
|
50 |
||
51 |
class JfrArtifactClosure { |
|
52 |
public: |
|
53 |
virtual void do_artifact(const void* artifact) = 0; |
|
54 |
}; |
|
55 |
||
56 |
template <typename T, typename Callback> |
|
57 |
class JfrArtifactCallbackHost : public JfrArtifactClosure { |
|
58 |
private: |
|
59 |
Callback* _callback; |
|
60 |
public: |
|
61 |
JfrArtifactCallbackHost(Callback* callback) : _callback(callback) {} |
|
62 |
void do_artifact(const void* artifact) { |
|
63 |
(*_callback)(reinterpret_cast<T const&>(artifact)); |
|
64 |
} |
|
65 |
}; |
|
66 |
||
67 |
template <typename FieldSelector, typename Letter> |
|
68 |
class KlassToFieldEnvelope { |
|
69 |
Letter* _letter; |
|
70 |
public: |
|
71 |
KlassToFieldEnvelope(Letter* letter) : _letter(letter) {} |
|
72 |
bool operator()(const Klass* klass) { |
|
73 |
typename FieldSelector::TypePtr t = FieldSelector::select(klass); |
|
74 |
return t != NULL ? (*_letter)(t) : true; |
|
75 |
} |
|
76 |
}; |
|
77 |
||
78 |
template <typename T> |
|
57360 | 79 |
void tag_leakp_artifact(T const& value, bool current_epoch) { |
50113 | 80 |
assert(value != NULL, "invariant"); |
57360 | 81 |
if (current_epoch) { |
50113 | 82 |
SET_LEAKP_USED_THIS_EPOCH(value); |
83 |
assert(LEAKP_USED_THIS_EPOCH(value), "invariant"); |
|
84 |
} else { |
|
85 |
SET_LEAKP_USED_PREV_EPOCH(value); |
|
86 |
assert(LEAKP_USED_PREV_EPOCH(value), "invariant"); |
|
87 |
} |
|
88 |
} |
|
89 |
||
90 |
template <typename T> |
|
91 |
class LeakpClearArtifact { |
|
57360 | 92 |
bool _current_epoch; |
50113 | 93 |
public: |
57360 | 94 |
LeakpClearArtifact(bool current_epoch) : _current_epoch(current_epoch) {} |
50113 | 95 |
bool operator()(T const& value) { |
57360 | 96 |
if (_current_epoch) { |
50113 | 97 |
if (LEAKP_USED_THIS_EPOCH(value)) { |
98 |
LEAKP_UNUSE_THIS_EPOCH(value); |
|
99 |
} |
|
100 |
} else { |
|
101 |
if (LEAKP_USED_PREV_EPOCH(value)) { |
|
102 |
LEAKP_UNUSE_PREV_EPOCH(value); |
|
103 |
} |
|
104 |
} |
|
105 |
return true; |
|
106 |
} |
|
107 |
}; |
|
108 |
||
109 |
template <typename T> |
|
57360 | 110 |
class UnTagArtifact { |
111 |
public: |
|
112 |
UnTagArtifact() {} |
|
113 |
bool operator()(T const& value) { |
|
114 |
if (LEAKP_USED_PREV_EPOCH(value)) { |
|
115 |
LEAKP_UNUSE_PREV_EPOCH(value); |
|
116 |
} |
|
117 |
if (USED_PREV_EPOCH(value)) { |
|
118 |
UNUSE_PREV_EPOCH(value); |
|
119 |
} |
|
120 |
return true; |
|
121 |
} |
|
122 |
}; |
|
123 |
||
124 |
template <typename T> |
|
50113 | 125 |
class ClearArtifact { |
126 |
public: |
|
127 |
bool operator()(T const& value) { |
|
57360 | 128 |
if (LEAKP_USED_PREV_EPOCH(value)) { |
129 |
LEAKP_UNUSE_PREV_EPOCH(value); |
|
130 |
} |
|
131 |
if (USED_PREV_EPOCH(value)) { |
|
132 |
UNUSE_PREV_EPOCH(value); |
|
133 |
} |
|
134 |
if (IS_SERIALIZED(value)) { |
|
135 |
UNSERIALIZE(value); |
|
50113 | 136 |
} |
57360 | 137 |
assert(IS_NOT_SERIALIZED(value), "invariant"); |
138 |
return true; |
|
139 |
} |
|
140 |
}; |
|
141 |
||
142 |
template <> |
|
143 |
class ClearArtifact<const Klass*> { |
|
144 |
public: |
|
145 |
bool operator()(const Klass* klass) { |
|
146 |
if (LEAKP_USED_PREV_EPOCH(klass)) { |
|
147 |
LEAKP_UNUSE_PREV_EPOCH(klass); |
|
148 |
} |
|
149 |
if (USED_PREV_EPOCH(klass)) { |
|
150 |
UNUSE_PREV_EPOCH(klass); |
|
151 |
} |
|
152 |
if (METHOD_USED_PREV_EPOCH(klass)) { |
|
153 |
UNUSE_METHOD_PREV_EPOCH(klass); |
|
154 |
} |
|
155 |
if (IS_SERIALIZED(klass)) { |
|
156 |
UNSERIALIZE(klass); |
|
157 |
} |
|
158 |
assert(IS_NOT_SERIALIZED(klass), "invariant"); |
|
50113 | 159 |
return true; |
160 |
} |
|
161 |
}; |
|
162 |
||
163 |
template <> |
|
164 |
class ClearArtifact<const Method*> { |
|
165 |
public: |
|
166 |
bool operator()(const Method* method) { |
|
57360 | 167 |
if (METHOD_FLAG_USED_PREV_EPOCH(method)) { |
168 |
CLEAR_METHOD_FLAG_USED_PREV_EPOCH(method); |
|
50113 | 169 |
} |
170 |
return true; |
|
171 |
} |
|
172 |
}; |
|
173 |
||
174 |
template <typename T> |
|
175 |
class LeakPredicate { |
|
57360 | 176 |
bool _current_epoch; |
50113 | 177 |
public: |
57360 | 178 |
LeakPredicate(bool current_epoch) : _current_epoch(current_epoch) {} |
50113 | 179 |
bool operator()(T const& value) { |
57360 | 180 |
return _current_epoch ? LEAKP_USED_THIS_EPOCH(value) : LEAKP_USED_PREV_EPOCH(value); |
181 |
} |
|
182 |
}; |
|
183 |
||
184 |
template <typename T> |
|
185 |
class LeakSerializePredicate { |
|
186 |
LeakPredicate<T> _leak_predicate; |
|
187 |
public: |
|
188 |
LeakSerializePredicate(bool current_epoch) : _leak_predicate(current_epoch) {} |
|
189 |
bool operator()(T const& value) { |
|
190 |
return IS_NOT_LEAKP_SERIALIZED(value) && _leak_predicate(value); |
|
50113 | 191 |
} |
192 |
}; |
|
193 |
||
194 |
template <typename T> |
|
195 |
class UsedPredicate { |
|
57360 | 196 |
bool _current_epoch; |
197 |
public: |
|
198 |
UsedPredicate(bool current_epoch) : _current_epoch(current_epoch) {} |
|
199 |
bool operator()(T const& value) { |
|
200 |
return _current_epoch ? USED_THIS_EPOCH(value) : USED_PREV_EPOCH(value); |
|
201 |
} |
|
202 |
}; |
|
203 |
||
204 |
template <typename T> |
|
205 |
class SerializePredicate { |
|
206 |
bool _current_epoch; |
|
50113 | 207 |
public: |
57360 | 208 |
SerializePredicate(bool current_epoch) : _current_epoch(current_epoch) {} |
50113 | 209 |
bool operator()(T const& value) { |
57360 | 210 |
assert(value != NULL, "invariant"); |
211 |
return IS_NOT_SERIALIZED(value); |
|
212 |
} |
|
213 |
}; |
|
214 |
||
215 |
template <> |
|
216 |
class SerializePredicate<const Method*> { |
|
217 |
bool _current_epoch; |
|
218 |
public: |
|
219 |
SerializePredicate(bool current_epoch) : _current_epoch(current_epoch) {} |
|
220 |
bool operator()(const Method* method) { |
|
221 |
assert(method != NULL, "invariant"); |
|
222 |
return METHOD_NOT_SERIALIZED(method); |
|
50113 | 223 |
} |
224 |
}; |
|
225 |
||
226 |
template <typename T, int compare(const T&, const T&)> |
|
227 |
class UniquePredicate { |
|
228 |
private: |
|
229 |
GrowableArray<T> _seen; |
|
230 |
public: |
|
231 |
UniquePredicate(bool) : _seen() {} |
|
232 |
bool operator()(T const& value) { |
|
233 |
bool not_unique; |
|
234 |
_seen.template find_sorted<T, compare>(value, not_unique); |
|
235 |
if (not_unique) { |
|
236 |
return false; |
|
237 |
} |
|
238 |
_seen.template insert_sorted<compare>(value); |
|
239 |
return true; |
|
240 |
} |
|
241 |
}; |
|
242 |
||
243 |
class MethodFlagPredicate { |
|
57360 | 244 |
bool _current_epoch; |
50113 | 245 |
public: |
57360 | 246 |
MethodFlagPredicate(bool current_epoch) : _current_epoch(current_epoch) {} |
50113 | 247 |
bool operator()(const Method* method) { |
57360 | 248 |
return _current_epoch ? METHOD_FLAG_USED_THIS_EPOCH(method) : METHOD_FLAG_USED_PREV_EPOCH(method); |
50113 | 249 |
} |
250 |
}; |
|
251 |
||
252 |
template <bool leakp> |
|
253 |
class MethodUsedPredicate { |
|
57360 | 254 |
bool _current_epoch; |
50113 | 255 |
public: |
57360 | 256 |
MethodUsedPredicate(bool current_epoch) : _current_epoch(current_epoch) {} |
50113 | 257 |
bool operator()(const Klass* klass) { |
258 |
assert(ANY_USED(klass), "invariant"); |
|
57360 | 259 |
if (_current_epoch) { |
50113 | 260 |
return leakp ? LEAKP_METHOD_USED_THIS_EPOCH(klass) : METHOD_USED_THIS_EPOCH(klass); |
261 |
} |
|
262 |
return leakp ? LEAKP_METHOD_USED_PREV_EPOCH(klass) : METHOD_USED_PREV_EPOCH(klass); |
|
263 |
} |
|
264 |
}; |
|
265 |
||
266 |
class JfrSymbolId : public JfrCHeapObj { |
|
267 |
template <typename, typename, template<typename, typename> class, typename, size_t> |
|
268 |
friend class HashTableHost; |
|
269 |
typedef HashTableHost<const Symbol*, traceid, Entry, JfrSymbolId> SymbolTable; |
|
270 |
typedef HashTableHost<const char*, traceid, Entry, JfrSymbolId> CStringTable; |
|
271 |
public: |
|
272 |
typedef SymbolTable::HashEntry SymbolEntry; |
|
273 |
typedef CStringTable::HashEntry CStringEntry; |
|
274 |
private: |
|
275 |
SymbolTable* _sym_table; |
|
276 |
CStringTable* _cstring_table; |
|
277 |
traceid _symbol_id_counter; |
|
278 |
||
279 |
// hashtable(s) callbacks |
|
280 |
void assign_id(SymbolEntry* entry); |
|
281 |
bool equals(const Symbol* query, uintptr_t hash, const SymbolEntry* entry); |
|
282 |
void assign_id(CStringEntry* entry); |
|
283 |
bool equals(const char* query, uintptr_t hash, const CStringEntry* entry); |
|
284 |
||
285 |
public: |
|
51444
3e5d28e6de32
8209301: JVM rename is_anonymous, host_klass to unsafe specific terminology ahead of Unsafe.defineAnonymousClass deprecation
lfoltan
parents:
50634
diff
changeset
|
286 |
static bool is_unsafe_anonymous_klass(const Klass* k); |
3e5d28e6de32
8209301: JVM rename is_anonymous, host_klass to unsafe specific terminology ahead of Unsafe.defineAnonymousClass deprecation
lfoltan
parents:
50634
diff
changeset
|
287 |
static const char* create_unsafe_anonymous_klass_symbol(const InstanceKlass* ik, uintptr_t& hashcode); |
3e5d28e6de32
8209301: JVM rename is_anonymous, host_klass to unsafe specific terminology ahead of Unsafe.defineAnonymousClass deprecation
lfoltan
parents:
50634
diff
changeset
|
288 |
static uintptr_t unsafe_anonymous_klass_name_hash_code(const InstanceKlass* ik); |
50113 | 289 |
static uintptr_t regular_klass_name_hash_code(const Klass* k); |
290 |
||
291 |
JfrSymbolId(); |
|
292 |
~JfrSymbolId(); |
|
293 |
||
294 |
void initialize(); |
|
295 |
void clear(); |
|
296 |
||
51444
3e5d28e6de32
8209301: JVM rename is_anonymous, host_klass to unsafe specific terminology ahead of Unsafe.defineAnonymousClass deprecation
lfoltan
parents:
50634
diff
changeset
|
297 |
traceid mark_unsafe_anonymous_klass_name(const Klass* k); |
50113 | 298 |
traceid mark(const Symbol* sym, uintptr_t hash); |
299 |
traceid mark(const Klass* k); |
|
300 |
traceid mark(const Symbol* symbol); |
|
301 |
traceid mark(const char* str, uintptr_t hash); |
|
302 |
||
303 |
const SymbolEntry* map_symbol(const Symbol* symbol) const; |
|
304 |
const SymbolEntry* map_symbol(uintptr_t hash) const; |
|
305 |
const CStringEntry* map_cstring(uintptr_t hash) const; |
|
306 |
||
307 |
template <typename T> |
|
308 |
void symbol(T& functor, const Klass* k) { |
|
51444
3e5d28e6de32
8209301: JVM rename is_anonymous, host_klass to unsafe specific terminology ahead of Unsafe.defineAnonymousClass deprecation
lfoltan
parents:
50634
diff
changeset
|
309 |
if (is_unsafe_anonymous_klass(k)) { |
50113 | 310 |
return; |
311 |
} |
|
312 |
functor(map_symbol(regular_klass_name_hash_code(k))); |
|
313 |
} |
|
314 |
||
315 |
template <typename T> |
|
316 |
void symbol(T& functor, const Method* method) { |
|
317 |
assert(method != NULL, "invariant"); |
|
318 |
functor(map_symbol((uintptr_t)method->name()->identity_hash())); |
|
319 |
functor(map_symbol((uintptr_t)method->signature()->identity_hash())); |
|
320 |
} |
|
321 |
||
322 |
template <typename T> |
|
323 |
void cstring(T& functor, const Klass* k) { |
|
51444
3e5d28e6de32
8209301: JVM rename is_anonymous, host_klass to unsafe specific terminology ahead of Unsafe.defineAnonymousClass deprecation
lfoltan
parents:
50634
diff
changeset
|
324 |
if (!is_unsafe_anonymous_klass(k)) { |
50113 | 325 |
return; |
326 |
} |
|
51444
3e5d28e6de32
8209301: JVM rename is_anonymous, host_klass to unsafe specific terminology ahead of Unsafe.defineAnonymousClass deprecation
lfoltan
parents:
50634
diff
changeset
|
327 |
functor(map_cstring(unsafe_anonymous_klass_name_hash_code((const InstanceKlass*)k))); |
50113 | 328 |
} |
329 |
||
330 |
template <typename T> |
|
331 |
void iterate_symbols(T& functor) { |
|
332 |
_sym_table->iterate_entry(functor); |
|
333 |
} |
|
334 |
||
335 |
template <typename T> |
|
336 |
void iterate_cstrings(T& functor) { |
|
337 |
_cstring_table->iterate_entry(functor); |
|
338 |
} |
|
339 |
||
340 |
bool has_entries() const { return has_symbol_entries() || has_cstring_entries(); } |
|
341 |
bool has_symbol_entries() const { return _sym_table->has_entries(); } |
|
342 |
bool has_cstring_entries() const { return _cstring_table->has_entries(); } |
|
343 |
}; |
|
344 |
||
345 |
/** |
|
346 |
* When processing a set of artifacts, there will be a need |
|
347 |
* to track transitive dependencies originating with each artifact. |
|
348 |
* These might or might not be explicitly "tagged" at that point. |
|
349 |
* With the introduction of "epochs" to allow for concurrent tagging, |
|
350 |
* we attempt to avoid "tagging" an artifact to indicate its use in a |
|
351 |
* previous epoch. This is mainly to reduce the risk for data races. |
|
352 |
* Instead, JfrArtifactSet is used to track transitive dependencies |
|
353 |
* during the write process itself. |
|
354 |
* |
|
355 |
* It can also provide opportunities for caching, as the ideal should |
|
356 |
* be to reduce the amount of iterations neccessary for locating artifacts |
|
357 |
* in the respective VM subsystems. |
|
358 |
*/ |
|
359 |
class JfrArtifactSet : public JfrCHeapObj { |
|
360 |
private: |
|
361 |
JfrSymbolId* _symbol_id; |
|
362 |
GrowableArray<const Klass*>* _klass_list; |
|
57360 | 363 |
size_t _total_count; |
364 |
bool _current_epoch; |
|
50113 | 365 |
|
366 |
public: |
|
57360 | 367 |
JfrArtifactSet(bool current_epoch); |
50113 | 368 |
~JfrArtifactSet(); |
369 |
||
370 |
// caller needs ResourceMark |
|
57360 | 371 |
void initialize(bool current_epoch); |
50113 | 372 |
void clear(); |
373 |
||
374 |
traceid mark(const Symbol* sym, uintptr_t hash); |
|
375 |
traceid mark(const Klass* klass); |
|
376 |
traceid mark(const Symbol* symbol); |
|
377 |
traceid mark(const char* const str, uintptr_t hash); |
|
51444
3e5d28e6de32
8209301: JVM rename is_anonymous, host_klass to unsafe specific terminology ahead of Unsafe.defineAnonymousClass deprecation
lfoltan
parents:
50634
diff
changeset
|
378 |
traceid mark_unsafe_anonymous_klass_name(const Klass* klass); |
50113 | 379 |
|
380 |
const JfrSymbolId::SymbolEntry* map_symbol(const Symbol* symbol) const; |
|
381 |
const JfrSymbolId::SymbolEntry* map_symbol(uintptr_t hash) const; |
|
382 |
const JfrSymbolId::CStringEntry* map_cstring(uintptr_t hash) const; |
|
383 |
||
384 |
bool has_klass_entries() const; |
|
57360 | 385 |
bool current_epoch() const { return _current_epoch; } |
50113 | 386 |
int entries() const; |
57360 | 387 |
size_t total_count() const; |
50113 | 388 |
void register_klass(const Klass* k); |
389 |
||
390 |
template <typename Functor> |
|
391 |
void iterate_klasses(Functor& functor) const { |
|
392 |
for (int i = 0; i < _klass_list->length(); ++i) { |
|
393 |
if (!functor(_klass_list->at(i))) { |
|
394 |
break; |
|
395 |
} |
|
396 |
} |
|
397 |
} |
|
398 |
||
399 |
template <typename T> |
|
400 |
void iterate_symbols(T& functor) { |
|
401 |
_symbol_id->iterate_symbols(functor); |
|
402 |
} |
|
403 |
||
404 |
template <typename T> |
|
405 |
void iterate_cstrings(T& functor) { |
|
406 |
_symbol_id->iterate_cstrings(functor); |
|
407 |
} |
|
57360 | 408 |
|
409 |
template <typename Writer> |
|
410 |
void tally(Writer& writer) { |
|
411 |
_total_count += writer.count(); |
|
412 |
} |
|
413 |
||
50113 | 414 |
}; |
415 |
||
416 |
class KlassArtifactRegistrator { |
|
417 |
private: |
|
418 |
JfrArtifactSet* _artifacts; |
|
419 |
public: |
|
420 |
KlassArtifactRegistrator(JfrArtifactSet* artifacts) : |
|
421 |
_artifacts(artifacts) { |
|
422 |
assert(_artifacts != NULL, "invariant"); |
|
423 |
} |
|
424 |
||
425 |
bool operator()(const Klass* klass) { |
|
426 |
assert(klass != NULL, "invariant"); |
|
427 |
_artifacts->register_klass(klass); |
|
428 |
return true; |
|
429 |
} |
|
430 |
}; |
|
431 |
||
53244
9807daeb47c4
8216167: Update include guards to reflect correct directories
coleenp
parents:
51444
diff
changeset
|
432 |
#endif // SHARE_JFR_RECORDER_CHECKPOINT_TYPES_JFRTYPESETUTILS_HPP |