author | mgronlun |
Mon, 16 Sep 2019 11:18:50 +0200 | |
branch | JEP-349-branch |
changeset 58164 | 1343c01b29d5 |
parent 58159 | 892527a70da9 |
child 58167 | 38b5442bcab4 |
permissions | -rw-r--r-- |
50113 | 1 |
/* |
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
53897
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 "jfrfiles/jfrEventClasses.hpp" |
50113 | 27 |
#include "jfr/jni/jfrJavaSupport.hpp" |
55571
49102ba8cf14
8214542: JFR: Old Object Sample event slow on a deep heap in debug builds
mgronlun
parents:
54623
diff
changeset
|
28 |
#include "jfr/leakprofiler/leakProfiler.hpp" |
50113 | 29 |
#include "jfr/leakprofiler/checkpoint/objectSampleCheckpoint.hpp" |
55571
49102ba8cf14
8214542: JFR: Old Object Sample event slow on a deep heap in debug builds
mgronlun
parents:
54623
diff
changeset
|
30 |
#include "jfr/leakprofiler/sampling/objectSampler.hpp" |
50113 | 31 |
#include "jfr/recorder/jfrRecorder.hpp" |
32 |
#include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp" |
|
33 |
#include "jfr/recorder/checkpoint/jfrMetadataEvent.hpp" |
|
53014
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52877
diff
changeset
|
34 |
#include "jfr/recorder/repository/jfrChunkRotation.hpp" |
50113 | 35 |
#include "jfr/recorder/repository/jfrChunkWriter.hpp" |
36 |
#include "jfr/recorder/repository/jfrRepository.hpp" |
|
37 |
#include "jfr/recorder/service/jfrPostBox.hpp" |
|
38 |
#include "jfr/recorder/service/jfrRecorderService.hpp" |
|
39 |
#include "jfr/recorder/stacktrace/jfrStackTraceRepository.hpp" |
|
40 |
#include "jfr/recorder/storage/jfrStorage.hpp" |
|
41 |
#include "jfr/recorder/storage/jfrStorageControl.hpp" |
|
42 |
#include "jfr/recorder/stringpool/jfrStringPool.hpp" |
|
43 |
#include "jfr/utilities/jfrAllocation.hpp" |
|
44 |
#include "jfr/utilities/jfrTime.hpp" |
|
45 |
#include "jfr/writers/jfrJavaEventWriter.hpp" |
|
46 |
#include "jfr/utilities/jfrTypes.hpp" |
|
47 |
#include "logging/log.hpp" |
|
48 |
#include "memory/resourceArea.hpp" |
|
49 |
#include "runtime/atomic.hpp" |
|
50 |
#include "runtime/handles.inline.hpp" |
|
51 |
#include "runtime/mutexLocker.hpp" |
|
50429
83aec1d357d4
8204301: Make OrderAccess functions available to hpp rather than inline.hpp files
coleenp
parents:
50113
diff
changeset
|
52 |
#include "runtime/orderAccess.hpp" |
50113 | 53 |
#include "runtime/os.hpp" |
54 |
#include "runtime/safepoint.hpp" |
|
55 |
#include "runtime/thread.inline.hpp" |
|
52877
9e041366c764
8214850: Rename vm_operations.?pp files to vmOperations.?pp files
tschatzl
parents:
51334
diff
changeset
|
56 |
#include "runtime/vmOperations.hpp" |
50113 | 57 |
#include "runtime/vmThread.hpp" |
58 |
||
59 |
// set data iff *dest == NULL |
|
60 |
static bool try_set(void* const data, void** dest, bool clear) { |
|
61 |
assert(data != NULL, "invariant"); |
|
58164 | 62 |
const void* const current = *dest; |
50113 | 63 |
if (current != NULL) { |
64 |
if (current != data) { |
|
65 |
// already set |
|
66 |
return false; |
|
67 |
} |
|
68 |
assert(current == data, "invariant"); |
|
69 |
if (!clear) { |
|
70 |
// recursion disallowed |
|
71 |
return false; |
|
72 |
} |
|
73 |
} |
|
74 |
return Atomic::cmpxchg(clear ? NULL : data, dest, current) == current; |
|
75 |
} |
|
76 |
||
77 |
static void* rotation_thread = NULL; |
|
78 |
static const int rotation_try_limit = 1000; |
|
79 |
static const int rotation_retry_sleep_millis = 10; |
|
80 |
||
57360 | 81 |
// incremented on each flushpoint |
82 |
static u8 flushpoint_id = 0; |
|
83 |
||
50113 | 84 |
class RotationLock : public StackObj { |
85 |
private: |
|
86 |
Thread* const _thread; |
|
87 |
bool _acquired; |
|
88 |
||
89 |
void log(bool recursion) { |
|
90 |
assert(!_acquired, "invariant"); |
|
91 |
const char* error_msg = NULL; |
|
92 |
if (recursion) { |
|
93 |
error_msg = "Unable to issue rotation due to recursive calls."; |
|
94 |
} |
|
95 |
else { |
|
96 |
error_msg = "Unable to issue rotation due to wait timeout."; |
|
97 |
} |
|
98 |
log_info(jfr)( // For user, should not be "jfr, system" |
|
99 |
"%s", error_msg); |
|
100 |
} |
|
101 |
public: |
|
102 |
RotationLock(Thread* thread) : _thread(thread), _acquired(false) { |
|
103 |
assert(_thread != NULL, "invariant"); |
|
104 |
if (_thread == rotation_thread) { |
|
105 |
// recursion not supported |
|
106 |
log(true); |
|
107 |
return; |
|
108 |
} |
|
109 |
||
110 |
// limited to not spin indefinitely |
|
111 |
for (int i = 0; i < rotation_try_limit; ++i) { |
|
112 |
if (try_set(_thread, &rotation_thread, false)) { |
|
113 |
_acquired = true; |
|
114 |
assert(_thread == rotation_thread, "invariant"); |
|
115 |
return; |
|
116 |
} |
|
117 |
if (_thread->is_Java_thread()) { |
|
118 |
// in order to allow the system to move to a safepoint |
|
54623
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
53897
diff
changeset
|
119 |
MutexLocker msg_lock(JfrMsg_lock); |
1126f0607c70
8222811: Consolidate MutexLockerEx and MutexLocker
coleenp
parents:
53897
diff
changeset
|
120 |
JfrMsg_lock->wait(rotation_retry_sleep_millis); |
50113 | 121 |
} |
122 |
else { |
|
123 |
os::naked_short_sleep(rotation_retry_sleep_millis); |
|
124 |
} |
|
125 |
} |
|
126 |
log(false); |
|
127 |
} |
|
128 |
||
129 |
~RotationLock() { |
|
130 |
assert(_thread != NULL, "invariant"); |
|
131 |
if (_acquired) { |
|
132 |
assert(_thread == rotation_thread, "invariant"); |
|
133 |
while (!try_set(_thread, &rotation_thread, true)); |
|
134 |
} |
|
135 |
} |
|
136 |
bool not_acquired() const { return !_acquired; } |
|
137 |
}; |
|
138 |
||
57360 | 139 |
template <typename E, typename Instance, size_t(Instance::*func)()> |
58068 | 140 |
class Content { |
57360 | 141 |
private: |
142 |
Instance& _instance; |
|
143 |
u4 _elements; |
|
144 |
public: |
|
145 |
typedef E EventType; |
|
58068 | 146 |
Content(Instance& instance) : _instance(instance), _elements(0) {} |
57360 | 147 |
bool process() { |
148 |
_elements = (u4)(_instance.*func)(); |
|
149 |
return true; |
|
150 |
} |
|
151 |
u4 elements() const { return _elements; } |
|
152 |
}; |
|
153 |
||
58068 | 154 |
template <typename Content> |
155 |
class WriteContent : public StackObj { |
|
57360 | 156 |
protected: |
157 |
const JfrTicks _start_time; |
|
158 |
JfrTicks _end_time; |
|
159 |
JfrChunkWriter& _cw; |
|
58068 | 160 |
Content& _content; |
57360 | 161 |
const int64_t _start_offset; |
162 |
public: |
|
58068 | 163 |
typedef typename Content::EventType EventType; |
57360 | 164 |
|
58068 | 165 |
WriteContent(JfrChunkWriter& cw, Content& content) : |
57360 | 166 |
_start_time(JfrTicks::now()), |
167 |
_end_time(), |
|
168 |
_cw(cw), |
|
58068 | 169 |
_content(content), |
57360 | 170 |
_start_offset(_cw.current_offset()) { |
171 |
assert(_cw.is_valid(), "invariant"); |
|
172 |
} |
|
173 |
||
174 |
bool process() { |
|
175 |
// invocation |
|
58068 | 176 |
_content.process(); |
57360 | 177 |
_end_time = JfrTicks::now(); |
58068 | 178 |
return 0 != _content.elements(); |
57360 | 179 |
} |
180 |
||
181 |
const JfrTicks& start_time() const { |
|
182 |
return _start_time; |
|
183 |
} |
|
184 |
||
185 |
const JfrTicks& end_time() const { |
|
186 |
return _end_time; |
|
187 |
} |
|
188 |
||
189 |
int64_t start_offset() const { |
|
190 |
return _start_offset; |
|
191 |
} |
|
192 |
||
193 |
int64_t end_offset() const { |
|
194 |
return current_offset(); |
|
195 |
} |
|
196 |
||
197 |
int64_t current_offset() const { |
|
198 |
return _cw.current_offset(); |
|
199 |
} |
|
200 |
||
201 |
u4 elements() const { |
|
58068 | 202 |
return (u4) _content.elements(); |
57360 | 203 |
} |
204 |
||
205 |
u4 size() const { |
|
206 |
return (u4)(end_offset() - start_offset()); |
|
207 |
} |
|
208 |
||
209 |
static bool is_event_enabled() { |
|
210 |
return EventType::is_enabled(); |
|
211 |
} |
|
212 |
||
213 |
static u8 event_id() { |
|
214 |
return EventType::eventId; |
|
215 |
} |
|
216 |
||
217 |
void write_elements(int64_t offset) { |
|
218 |
_cw.write_padded_at_offset<u4>(elements(), offset); |
|
219 |
} |
|
220 |
||
221 |
void write_size() { |
|
222 |
_cw.write_padded_at_offset<u4>(size(), start_offset()); |
|
223 |
} |
|
224 |
||
225 |
void set_last_checkpoint() { |
|
226 |
_cw.set_last_checkpoint_offset(start_offset()); |
|
227 |
} |
|
228 |
||
229 |
void rewind() { |
|
230 |
_cw.seek(start_offset()); |
|
231 |
} |
|
232 |
}; |
|
233 |
||
53897
0abec72a3ac2
8217647: JFR: recordings on 32-bit systems unreadable
bulasevich
parents:
53014
diff
changeset
|
234 |
static int64_t write_checkpoint_event_prologue(JfrChunkWriter& cw, u8 type_id) { |
58132
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
235 |
const int64_t last_cp_offset = cw.last_checkpoint_offset(); |
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
236 |
const int64_t delta_to_last_checkpoint = 0 == last_cp_offset ? 0 : last_cp_offset - cw.current_offset(); |
50113 | 237 |
cw.reserve(sizeof(u4)); |
238 |
cw.write<u8>(EVENT_CHECKPOINT); |
|
239 |
cw.write(JfrTicks::now()); |
|
58132
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
240 |
cw.write((int64_t)0); // duration |
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
241 |
cw.write(delta_to_last_checkpoint); |
50113 | 242 |
cw.write<bool>(false); // flushpoint |
53897
0abec72a3ac2
8217647: JFR: recordings on 32-bit systems unreadable
bulasevich
parents:
53014
diff
changeset
|
243 |
cw.write((u4)1); // nof types in this checkpoint |
0abec72a3ac2
8217647: JFR: recordings on 32-bit systems unreadable
bulasevich
parents:
53014
diff
changeset
|
244 |
cw.write(type_id); |
0abec72a3ac2
8217647: JFR: recordings on 32-bit systems unreadable
bulasevich
parents:
53014
diff
changeset
|
245 |
const int64_t number_of_elements_offset = cw.current_offset(); |
50113 | 246 |
cw.reserve(sizeof(u4)); |
247 |
return number_of_elements_offset; |
|
248 |
} |
|
249 |
||
58068 | 250 |
template <typename Content> |
251 |
class WriteCheckpointEvent : public WriteContent<Content> { |
|
50113 | 252 |
private: |
57360 | 253 |
const u8 _type_id; |
50113 | 254 |
public: |
58068 | 255 |
WriteCheckpointEvent(JfrChunkWriter& cw, Content& content, u8 type_id) : |
256 |
WriteContent<Content>(cw, content), _type_id(type_id) {} |
|
57360 | 257 |
|
50113 | 258 |
bool process() { |
57360 | 259 |
const int64_t num_elements_offset = write_checkpoint_event_prologue(this->_cw, _type_id); |
58068 | 260 |
if (!WriteContent<Content>::process()) { |
50113 | 261 |
// nothing to do, rewind writer to start |
57360 | 262 |
this->rewind(); |
263 |
assert(this->current_offset() == this->start_offset(), "invariant"); |
|
264 |
return false; |
|
50113 | 265 |
} |
57360 | 266 |
assert(this->elements() > 0, "invariant"); |
267 |
assert(this->current_offset() > num_elements_offset, "invariant"); |
|
268 |
this->write_elements(num_elements_offset); |
|
269 |
this->write_size(); |
|
270 |
this->set_last_checkpoint(); |
|
50113 | 271 |
return true; |
272 |
} |
|
273 |
}; |
|
274 |
||
57360 | 275 |
template <typename Functor> |
58068 | 276 |
static u4 invoke(Functor& f) { |
277 |
f.process(); |
|
278 |
return f.elements(); |
|
279 |
} |
|
280 |
||
281 |
template <typename Functor> |
|
57360 | 282 |
static void write_flush_event(Functor& f) { |
57870
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
57360
diff
changeset
|
283 |
if (Functor::is_event_enabled()) { |
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
57360
diff
changeset
|
284 |
typename Functor::EventType e(UNTIMED); |
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
57360
diff
changeset
|
285 |
e.set_starttime(f.start_time()); |
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
57360
diff
changeset
|
286 |
e.set_endtime(f.end_time()); |
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
57360
diff
changeset
|
287 |
e.set_flushId(flushpoint_id); |
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
57360
diff
changeset
|
288 |
e.set_elements(f.elements()); |
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
57360
diff
changeset
|
289 |
e.set_size(f.size()); |
00860d9caf4d
New metadata system for oldobjects built on top of simplified tagging model. Caching and serialization improvements. Flushpoint checkpoint with chunkheader contents.
mgronlun
parents:
57360
diff
changeset
|
290 |
e.commit(); |
50113 | 291 |
} |
57360 | 292 |
} |
293 |
||
294 |
template <typename Functor> |
|
295 |
static u4 invoke_with_flush_event(Functor& f) { |
|
296 |
const u4 elements = invoke(f); |
|
57872 | 297 |
write_flush_event(f); |
57360 | 298 |
return elements; |
299 |
} |
|
50113 | 300 |
|
58068 | 301 |
class StackTraceRepository : public StackObj { |
50113 | 302 |
private: |
58068 | 303 |
JfrStackTraceRepository& _repo; |
304 |
JfrChunkWriter& _cw; |
|
305 |
size_t _elements; |
|
306 |
bool _clear; |
|
307 |
||
50113 | 308 |
public: |
58068 | 309 |
typedef EventFlushStacktrace EventType; |
310 |
StackTraceRepository(JfrStackTraceRepository& repo, JfrChunkWriter& cw, bool clear) : |
|
311 |
_repo(repo), _cw(cw), _elements(0), _clear(clear) {} |
|
50113 | 312 |
bool process() { |
58068 | 313 |
_elements = _repo.write(_cw, _clear); |
50113 | 314 |
return true; |
315 |
} |
|
58068 | 316 |
size_t elements() const { return _elements; } |
317 |
void reset() { _elements = 0; } |
|
50113 | 318 |
}; |
319 |
||
58068 | 320 |
typedef WriteCheckpointEvent<StackTraceRepository> WriteStackTrace; |
321 |
||
322 |
static u4 flush_stacktrace(JfrStackTraceRepository& stack_trace_repo, JfrChunkWriter& chunkwriter) { |
|
323 |
StackTraceRepository str(stack_trace_repo, chunkwriter, false); |
|
324 |
WriteStackTrace wst(chunkwriter, str, TYPE_STACKTRACE); |
|
325 |
return invoke_with_flush_event(wst); |
|
326 |
} |
|
327 |
||
328 |
static u4 write_stacktrace(JfrStackTraceRepository& stack_trace_repo, JfrChunkWriter& chunkwriter, bool clear) { |
|
329 |
StackTraceRepository str(stack_trace_repo, chunkwriter, clear); |
|
330 |
WriteStackTrace wst(chunkwriter, str, TYPE_STACKTRACE); |
|
331 |
return invoke(wst); |
|
332 |
} |
|
333 |
||
334 |
typedef Content<EventFlushStorage, JfrStorage, &JfrStorage::write> Storage; |
|
335 |
typedef WriteContent<Storage> WriteStorage; |
|
336 |
||
337 |
static size_t flush_storage(JfrStorage& storage, JfrChunkWriter& chunkwriter) { |
|
338 |
assert(chunkwriter.is_valid(), "invariant"); |
|
339 |
Storage fsf(storage); |
|
340 |
WriteStorage fs(chunkwriter, fsf); |
|
341 |
return invoke_with_flush_event(fs); |
|
342 |
} |
|
343 |
||
344 |
static size_t write_storage(JfrStorage& storage, JfrChunkWriter& chunkwriter) { |
|
345 |
assert(chunkwriter.is_valid(), "invariant"); |
|
346 |
Storage fsf(storage); |
|
347 |
WriteStorage fs(chunkwriter, fsf); |
|
348 |
return invoke(fs); |
|
349 |
} |
|
350 |
||
351 |
typedef Content<EventFlushStringPool, JfrStringPool, &JfrStringPool::write> StringPool; |
|
352 |
typedef Content<EventFlushStringPool, JfrStringPool, &JfrStringPool::write_at_safepoint> StringPoolSafepoint; |
|
353 |
typedef WriteCheckpointEvent<StringPool> WriteStringPool; |
|
354 |
typedef WriteCheckpointEvent<StringPoolSafepoint> WriteStringPoolSafepoint; |
|
355 |
||
356 |
static u4 flush_stringpool(JfrStringPool& string_pool, JfrChunkWriter& chunkwriter) { |
|
357 |
StringPool sp(string_pool); |
|
358 |
WriteStringPool wsp(chunkwriter, sp, TYPE_STRING); |
|
359 |
return invoke_with_flush_event(wsp); |
|
360 |
} |
|
361 |
||
362 |
static u4 write_stringpool(JfrStringPool& string_pool, JfrChunkWriter& chunkwriter) { |
|
363 |
StringPool sp(string_pool); |
|
364 |
WriteStringPool wsp(chunkwriter, sp, TYPE_STRING); |
|
365 |
return invoke(wsp); |
|
366 |
} |
|
367 |
||
368 |
static u4 write_stringpool_safepoint(JfrStringPool& string_pool, JfrChunkWriter& chunkwriter) { |
|
369 |
StringPoolSafepoint sps(string_pool); |
|
370 |
WriteStringPoolSafepoint wsps(chunkwriter, sps, TYPE_STRING); |
|
371 |
return invoke(wsps); |
|
372 |
} |
|
373 |
||
374 |
typedef Content<EventFlushTypeSet, JfrCheckpointManager, &JfrCheckpointManager::flush_type_set> FlushTypeSetFunctor; |
|
375 |
typedef WriteContent<FlushTypeSetFunctor> FlushTypeSet; |
|
376 |
||
377 |
static u4 flush_typeset(JfrCheckpointManager& checkpoint_manager, JfrChunkWriter& chunkwriter) { |
|
378 |
FlushTypeSetFunctor flush_type_set(checkpoint_manager); |
|
379 |
FlushTypeSet fts(chunkwriter, flush_type_set); |
|
380 |
return invoke_with_flush_event(fts); |
|
381 |
} |
|
382 |
||
383 |
class MetadataEvent : public StackObj { |
|
384 |
private: |
|
385 |
JfrChunkWriter& _cw; |
|
386 |
public: |
|
387 |
typedef EventFlushMetadata EventType; |
|
388 |
MetadataEvent(JfrChunkWriter& cw) : _cw(cw) {} |
|
389 |
bool process() { |
|
390 |
JfrMetadataEvent::write(_cw); |
|
391 |
return true; |
|
392 |
} |
|
393 |
size_t elements() const { return 1; } |
|
394 |
}; |
|
395 |
||
396 |
typedef WriteContent<MetadataEvent> WriteMetadata; |
|
397 |
||
398 |
static u4 flush_metadata(JfrChunkWriter& chunkwriter) { |
|
399 |
assert(chunkwriter.is_valid(), "invariant"); |
|
400 |
MetadataEvent me(chunkwriter); |
|
401 |
WriteMetadata wm(chunkwriter, me); |
|
402 |
return invoke_with_flush_event(wm); |
|
403 |
} |
|
404 |
||
405 |
static u4 write_metadata(JfrChunkWriter& chunkwriter) { |
|
406 |
assert(chunkwriter.is_valid(), "invariant"); |
|
407 |
MetadataEvent me(chunkwriter); |
|
408 |
WriteMetadata wm(chunkwriter, me); |
|
409 |
return invoke(wm); |
|
410 |
} |
|
411 |
||
50113 | 412 |
template <typename Instance, void(Instance::*func)()> |
413 |
class JfrVMOperation : public VM_Operation { |
|
414 |
private: |
|
415 |
Instance& _instance; |
|
416 |
public: |
|
417 |
JfrVMOperation(Instance& instance) : _instance(instance) {} |
|
418 |
void doit() { (_instance.*func)(); } |
|
419 |
VMOp_Type type() const { return VMOp_JFRCheckpoint; } |
|
420 |
Mode evaluation_mode() const { return _safepoint; } // default |
|
421 |
}; |
|
422 |
||
58068 | 423 |
JfrRecorderService::JfrRecorderService() : |
424 |
_checkpoint_manager(JfrCheckpointManager::instance()), |
|
425 |
_chunkwriter(JfrRepository::chunkwriter()), |
|
426 |
_repository(JfrRepository::instance()), |
|
427 |
_stack_trace_repository(JfrStackTraceRepository::instance()), |
|
428 |
_storage(JfrStorage::instance()), |
|
429 |
_string_pool(JfrStringPool::instance()) {} |
|
50113 | 430 |
|
431 |
static bool recording = false; |
|
432 |
||
433 |
static void set_recording_state(bool is_recording) { |
|
434 |
OrderAccess::storestore(); |
|
435 |
recording = is_recording; |
|
436 |
} |
|
437 |
||
438 |
bool JfrRecorderService::is_recording() { |
|
439 |
return recording; |
|
440 |
} |
|
441 |
||
442 |
void JfrRecorderService::start() { |
|
443 |
RotationLock rl(Thread::current()); |
|
444 |
if (rl.not_acquired()) { |
|
445 |
return; |
|
446 |
} |
|
447 |
log_debug(jfr, system)("Request to START recording"); |
|
448 |
assert(!is_recording(), "invariant"); |
|
449 |
clear(); |
|
450 |
set_recording_state(true); |
|
451 |
assert(is_recording(), "invariant"); |
|
452 |
open_new_chunk(); |
|
453 |
log_debug(jfr, system)("Recording STARTED"); |
|
454 |
} |
|
455 |
||
456 |
void JfrRecorderService::clear() { |
|
457 |
ResourceMark rm; |
|
458 |
HandleMark hm; |
|
459 |
pre_safepoint_clear(); |
|
460 |
invoke_safepoint_clear(); |
|
461 |
post_safepoint_clear(); |
|
462 |
} |
|
463 |
||
464 |
void JfrRecorderService::pre_safepoint_clear() { |
|
465 |
_stack_trace_repository.clear(); |
|
466 |
_string_pool.clear(); |
|
467 |
_storage.clear(); |
|
468 |
} |
|
469 |
||
470 |
void JfrRecorderService::invoke_safepoint_clear() { |
|
471 |
JfrVMOperation<JfrRecorderService, &JfrRecorderService::safepoint_clear> safepoint_task(*this); |
|
472 |
VMThread::execute(&safepoint_task); |
|
473 |
} |
|
474 |
||
475 |
void JfrRecorderService::safepoint_clear() { |
|
476 |
assert(SafepointSynchronize::is_at_safepoint(), "invariant"); |
|
477 |
_stack_trace_repository.clear(); |
|
478 |
_string_pool.clear(); |
|
479 |
_storage.clear(); |
|
480 |
_checkpoint_manager.shift_epoch(); |
|
57887 | 481 |
_chunkwriter.set_time_stamp(); |
50113 | 482 |
} |
483 |
||
484 |
void JfrRecorderService::post_safepoint_clear() { |
|
485 |
_checkpoint_manager.clear(); |
|
486 |
} |
|
487 |
||
58068 | 488 |
void JfrRecorderService::open_new_chunk(bool vm_error) { |
489 |
JfrChunkRotation::on_rotation(); |
|
490 |
const bool valid_chunk = _repository.open_chunk(vm_error); |
|
491 |
_storage.control().set_to_disk(valid_chunk); |
|
492 |
if (valid_chunk) { |
|
493 |
_checkpoint_manager.write_constants(); |
|
494 |
} |
|
495 |
} |
|
496 |
||
50113 | 497 |
static void stop() { |
498 |
assert(JfrRecorderService::is_recording(), "invariant"); |
|
499 |
log_debug(jfr, system)("Recording STOPPED"); |
|
500 |
set_recording_state(false); |
|
501 |
assert(!JfrRecorderService::is_recording(), "invariant"); |
|
502 |
} |
|
503 |
||
58068 | 504 |
void JfrRecorderService::prepare_for_vm_error_rotation() { |
505 |
if (!_chunkwriter.is_valid()) { |
|
506 |
open_new_chunk(true); |
|
507 |
} |
|
508 |
_checkpoint_manager.register_service_thread(Thread::current()); |
|
509 |
} |
|
510 |
||
511 |
void JfrRecorderService::vm_error_rotation() { |
|
512 |
if (_chunkwriter.is_valid()) { |
|
513 |
pre_safepoint_write(); |
|
514 |
// Do not attempt safepoint dependent operations during emergency dump. |
|
515 |
// Optimistically write tagged artifacts. |
|
516 |
_checkpoint_manager.shift_epoch(); |
|
517 |
// update time |
|
518 |
_chunkwriter.set_time_stamp(); |
|
519 |
post_safepoint_write(); |
|
520 |
assert(!_chunkwriter.is_valid(), "invariant"); |
|
521 |
_repository.on_vm_error(); |
|
522 |
} |
|
523 |
} |
|
524 |
||
50113 | 525 |
void JfrRecorderService::rotate(int msgs) { |
526 |
RotationLock rl(Thread::current()); |
|
527 |
if (rl.not_acquired()) { |
|
528 |
return; |
|
529 |
} |
|
530 |
static bool vm_error = false; |
|
531 |
if (msgs & MSGBIT(MSG_VM_ERROR)) { |
|
532 |
vm_error = true; |
|
533 |
prepare_for_vm_error_rotation(); |
|
534 |
} |
|
58132
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
535 |
if (!_storage.control().to_disk()) { |
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
536 |
in_memory_rotation(); |
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
537 |
} else if (vm_error) { |
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
538 |
vm_error_rotation(); |
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
539 |
} else { |
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
540 |
chunk_rotation(); |
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
541 |
} |
50113 | 542 |
if (msgs & (MSGBIT(MSG_STOP))) { |
543 |
stop(); |
|
544 |
} |
|
545 |
} |
|
546 |
||
547 |
void JfrRecorderService::in_memory_rotation() { |
|
548 |
// currently running an in-memory recording |
|
57360 | 549 |
assert(!_storage.control().to_disk(), "invariant"); |
50113 | 550 |
open_new_chunk(); |
551 |
if (_chunkwriter.is_valid()) { |
|
552 |
// dump all in-memory buffer data to the newly created chunk |
|
58068 | 553 |
write_storage(_storage, _chunkwriter); |
50113 | 554 |
} |
555 |
} |
|
556 |
||
557 |
void JfrRecorderService::chunk_rotation() { |
|
558 |
finalize_current_chunk(); |
|
559 |
open_new_chunk(); |
|
560 |
} |
|
561 |
||
562 |
void JfrRecorderService::finalize_current_chunk() { |
|
563 |
assert(_chunkwriter.is_valid(), "invariant"); |
|
564 |
write(); |
|
565 |
} |
|
566 |
||
567 |
void JfrRecorderService::write() { |
|
568 |
ResourceMark rm; |
|
569 |
HandleMark hm; |
|
570 |
pre_safepoint_write(); |
|
571 |
invoke_safepoint_write(); |
|
572 |
post_safepoint_write(); |
|
573 |
} |
|
574 |
||
575 |
void JfrRecorderService::pre_safepoint_write() { |
|
576 |
assert(_chunkwriter.is_valid(), "invariant"); |
|
58068 | 577 |
if (_stack_trace_repository.is_modified()) { |
578 |
write_stacktrace(_stack_trace_repository, _chunkwriter, false); |
|
579 |
} |
|
580 |
if (_string_pool.is_modified()) { |
|
581 |
write_stringpool(_string_pool, _chunkwriter); |
|
582 |
} |
|
55571
49102ba8cf14
8214542: JFR: Old Object Sample event slow on a deep heap in debug builds
mgronlun
parents:
54623
diff
changeset
|
583 |
if (LeakProfiler::is_running()) { |
49102ba8cf14
8214542: JFR: Old Object Sample event slow on a deep heap in debug builds
mgronlun
parents:
54623
diff
changeset
|
584 |
// Exclusive access to the object sampler instance. |
49102ba8cf14
8214542: JFR: Old Object Sample event slow on a deep heap in debug builds
mgronlun
parents:
54623
diff
changeset
|
585 |
// The sampler is released (unlocked) later in post_safepoint_write. |
58132
caa25ab47aca
8225797: OldObjectSample event creates unexpected amount of checkpoint data
mgronlun
parents:
55571
diff
changeset
|
586 |
ObjectSampleCheckpoint::on_rotation(ObjectSampler::acquire(), _stack_trace_repository); |
55571
49102ba8cf14
8214542: JFR: Old Object Sample event slow on a deep heap in debug builds
mgronlun
parents:
54623
diff
changeset
|
587 |
} |
58068 | 588 |
write_storage(_storage, _chunkwriter); |
50113 | 589 |
} |
590 |
||
591 |
void JfrRecorderService::invoke_safepoint_write() { |
|
592 |
JfrVMOperation<JfrRecorderService, &JfrRecorderService::safepoint_write> safepoint_task(*this); |
|
593 |
VMThread::execute(&safepoint_task); |
|
594 |
} |
|
595 |
||
596 |
void JfrRecorderService::safepoint_write() { |
|
597 |
assert(SafepointSynchronize::is_at_safepoint(), "invariant"); |
|
58068 | 598 |
write_stacktrace(_stack_trace_repository, _chunkwriter, true); |
599 |
if (_string_pool.is_modified()) { |
|
600 |
write_stringpool_safepoint(_string_pool, _chunkwriter); |
|
601 |
} |
|
50113 | 602 |
_storage.write_at_safepoint(); |
58068 | 603 |
_checkpoint_manager.notify_threads(); |
58154 | 604 |
_checkpoint_manager.notify_types_on_rotation(); |
50113 | 605 |
_checkpoint_manager.shift_epoch(); |
58068 | 606 |
_chunkwriter.set_time_stamp(); |
50113 | 607 |
} |
608 |
||
609 |
void JfrRecorderService::post_safepoint_write() { |
|
610 |
assert(_chunkwriter.is_valid(), "invariant"); |
|
611 |
// During the safepoint tasks just completed, the system transitioned to a new epoch. |
|
612 |
// Type tagging is epoch relative which entails we are able to write out the |
|
613 |
// already tagged artifacts for the previous epoch. We can accomplish this concurrently |
|
614 |
// with threads now tagging artifacts in relation to the new, now updated, epoch and remain outside of a safepoint. |
|
615 |
_checkpoint_manager.write_type_set(); |
|
55571
49102ba8cf14
8214542: JFR: Old Object Sample event slow on a deep heap in debug builds
mgronlun
parents:
54623
diff
changeset
|
616 |
if (LeakProfiler::is_running()) { |
49102ba8cf14
8214542: JFR: Old Object Sample event slow on a deep heap in debug builds
mgronlun
parents:
54623
diff
changeset
|
617 |
// The object sampler instance was exclusively acquired and locked in pre_safepoint_write. |
49102ba8cf14
8214542: JFR: Old Object Sample event slow on a deep heap in debug builds
mgronlun
parents:
54623
diff
changeset
|
618 |
// Note: There is a dependency on write_type_set() above, ensure the release is subsequent. |
49102ba8cf14
8214542: JFR: Old Object Sample event slow on a deep heap in debug builds
mgronlun
parents:
54623
diff
changeset
|
619 |
ObjectSampler::release(); |
49102ba8cf14
8214542: JFR: Old Object Sample event slow on a deep heap in debug builds
mgronlun
parents:
54623
diff
changeset
|
620 |
} |
50113 | 621 |
// serialize any outstanding checkpoint memory |
622 |
_checkpoint_manager.write(); |
|
623 |
// serialize the metadata descriptor event and close out the chunk |
|
58068 | 624 |
write_metadata(_chunkwriter); |
625 |
_repository.close_chunk(); |
|
50113 | 626 |
} |
627 |
||
57360 | 628 |
static JfrBuffer* thread_local_buffer() { |
629 |
return Thread::current()->jfr_thread_local()->native_buffer(); |
|
630 |
} |
|
631 |
||
632 |
static void reset_buffer(JfrBuffer* buffer) { |
|
633 |
assert(buffer != NULL, "invariant"); |
|
634 |
assert(buffer == thread_local_buffer(), "invariant"); |
|
635 |
buffer->set_pos(const_cast<u1*>(buffer->top())); |
|
636 |
assert(buffer->empty(), "invariant"); |
|
637 |
} |
|
638 |
||
639 |
static void reset_thread_local_buffer() { |
|
640 |
reset_buffer(thread_local_buffer()); |
|
641 |
} |
|
642 |
||
643 |
static void write_thread_local_buffer(JfrChunkWriter& chunkwriter) { |
|
644 |
JfrBuffer * const buffer = thread_local_buffer(); |
|
645 |
assert(buffer != NULL, "invariant"); |
|
646 |
if (!buffer->empty()) { |
|
647 |
chunkwriter.write_unbuffered(buffer->top(), buffer->pos() - buffer->top()); |
|
648 |
reset_buffer(buffer); |
|
649 |
} |
|
650 |
assert(buffer->empty(), "invariant"); |
|
50113 | 651 |
} |
652 |
||
57360 | 653 |
static bool write_metadata_in_flushpoint = false; |
654 |
||
655 |
size_t JfrRecorderService::flush() { |
|
656 |
size_t total_elements = 0; |
|
657 |
if (write_metadata_in_flushpoint) { |
|
58068 | 658 |
total_elements = flush_metadata(_chunkwriter); |
57360 | 659 |
} |
660 |
const size_t storage_elements = flush_storage(_storage, _chunkwriter); |
|
661 |
if (0 == storage_elements) { |
|
662 |
return total_elements; |
|
50113 | 663 |
} |
57360 | 664 |
total_elements += storage_elements; |
57872 | 665 |
if (_stack_trace_repository.is_modified()) { |
666 |
total_elements += flush_stacktrace(_stack_trace_repository, _chunkwriter); |
|
667 |
} |
|
668 |
if (_string_pool.is_modified()) { |
|
57360 | 669 |
total_elements += flush_stringpool(_string_pool, _chunkwriter); |
670 |
} |
|
57934 | 671 |
if (_checkpoint_manager.is_type_set_required()) { |
57873 | 672 |
total_elements += flush_typeset(_checkpoint_manager, _chunkwriter); |
57934 | 673 |
} else if (_checkpoint_manager.is_constant_set_required()) { |
674 |
// don't tally this, it is only in order to flush the waiting constants |
|
675 |
_checkpoint_manager.flush_constant_set(); |
|
57873 | 676 |
} |
57360 | 677 |
return total_elements; |
50113 | 678 |
} |
679 |
||
58068 | 680 |
typedef Content<EventFlush, JfrRecorderService, &JfrRecorderService::flush> FlushFunctor; |
681 |
typedef WriteContent<FlushFunctor> Flush; |
|
682 |
||
57360 | 683 |
void JfrRecorderService::flush(int msgs) { |
50113 | 684 |
assert(_chunkwriter.is_valid(), "invariant"); |
57360 | 685 |
ResourceMark rm; |
686 |
HandleMark hm; |
|
57872 | 687 |
write_metadata_in_flushpoint = (msgs & MSGBIT(MSG_FLUSHPOINT_METADATA)); |
57360 | 688 |
++flushpoint_id; |
57872 | 689 |
reset_thread_local_buffer(); |
57360 | 690 |
FlushFunctor flushpoint(*this); |
691 |
Flush fl(_chunkwriter, flushpoint); |
|
692 |
invoke_with_flush_event(fl); |
|
693 |
write_thread_local_buffer(_chunkwriter); |
|
694 |
_repository.flush_chunk(); |
|
50113 | 695 |
} |
696 |
||
697 |
void JfrRecorderService::process_full_buffers() { |
|
698 |
if (_chunkwriter.is_valid()) { |
|
699 |
_storage.write_full(); |
|
700 |
} |
|
701 |
} |
|
702 |
||
703 |
void JfrRecorderService::scavenge() { |
|
704 |
_storage.scavenge(); |
|
705 |
} |
|
706 |
||
707 |
void JfrRecorderService::evaluate_chunk_size_for_rotation() { |
|
53014
339d2fbe8675
8215284: Reduce noise induced by periodic task getFileSize()
mgronlun
parents:
52877
diff
changeset
|
708 |
JfrChunkRotation::evaluate(_chunkwriter); |
50113 | 709 |
} |