50113
|
1 |
/*
|
|
2 |
* Copyright (c) 2016, 2018, 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 "classfile/javaClasses.inline.hpp"
|
|
27 |
#include "jfr/recorder/jfrRecorder.hpp"
|
|
28 |
#include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"
|
|
29 |
#include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"
|
|
30 |
#include "jfr/recorder/checkpoint/types/jfrTypeManager.hpp"
|
|
31 |
#include "jfr/recorder/checkpoint/types/traceid/jfrTraceIdEpoch.hpp"
|
|
32 |
#include "jfr/recorder/service/jfrOptionSet.hpp"
|
|
33 |
#include "jfr/recorder/storage/jfrMemorySpace.inline.hpp"
|
|
34 |
#include "jfr/recorder/storage/jfrStorageUtils.inline.hpp"
|
|
35 |
#include "jfr/recorder/repository/jfrChunkWriter.hpp"
|
|
36 |
#include "jfr/utilities/jfrBigEndian.hpp"
|
|
37 |
#include "jfr/utilities/jfrTypes.hpp"
|
|
38 |
#include "logging/log.hpp"
|
|
39 |
#include "memory/resourceArea.hpp"
|
|
40 |
#include "runtime/mutexLocker.hpp"
|
|
41 |
#include "runtime/orderAccess.inline.hpp"
|
|
42 |
#include "runtime/safepoint.hpp"
|
|
43 |
|
|
44 |
typedef JfrCheckpointManager::Buffer* BufferPtr;
|
|
45 |
|
|
46 |
static JfrCheckpointManager* _instance = NULL;
|
|
47 |
|
|
48 |
JfrCheckpointManager& JfrCheckpointManager::instance() {
|
|
49 |
return *_instance;
|
|
50 |
}
|
|
51 |
|
|
52 |
JfrCheckpointManager* JfrCheckpointManager::create(JfrChunkWriter& cw) {
|
|
53 |
assert(_instance == NULL, "invariant");
|
|
54 |
_instance = new JfrCheckpointManager(cw);
|
|
55 |
return _instance;
|
|
56 |
}
|
|
57 |
|
|
58 |
void JfrCheckpointManager::destroy() {
|
|
59 |
assert(_instance != NULL, "invariant");
|
|
60 |
delete _instance;
|
|
61 |
_instance = NULL;
|
|
62 |
}
|
|
63 |
|
|
64 |
JfrCheckpointManager::JfrCheckpointManager(JfrChunkWriter& cw) :
|
|
65 |
_free_list_mspace(NULL),
|
|
66 |
_epoch_transition_mspace(NULL),
|
|
67 |
_lock(NULL),
|
|
68 |
_type_manager(NULL),
|
|
69 |
_service_thread(NULL),
|
|
70 |
_chunkwriter(cw),
|
|
71 |
_checkpoint_epoch_state(JfrTraceIdEpoch::epoch()) {}
|
|
72 |
|
|
73 |
JfrCheckpointManager::~JfrCheckpointManager() {
|
|
74 |
if (_free_list_mspace != NULL) {
|
|
75 |
delete _free_list_mspace;
|
|
76 |
}
|
|
77 |
if (_epoch_transition_mspace != NULL) {
|
|
78 |
delete _epoch_transition_mspace;
|
|
79 |
}
|
|
80 |
if (_lock != NULL) {
|
|
81 |
delete _lock;
|
|
82 |
}
|
|
83 |
if (_type_manager) {
|
|
84 |
delete _type_manager;
|
|
85 |
}
|
|
86 |
}
|
|
87 |
|
|
88 |
static const size_t unlimited_mspace_size = 0;
|
|
89 |
static const size_t checkpoint_buffer_cache_count = 2;
|
|
90 |
static const size_t checkpoint_buffer_size = 512 * K;
|
|
91 |
|
|
92 |
static JfrCheckpointMspace* create_mspace(size_t buffer_size, size_t limit, size_t cache_count, JfrCheckpointManager* system) {
|
|
93 |
JfrCheckpointMspace* mspace = new JfrCheckpointMspace(buffer_size, limit, cache_count, system);
|
|
94 |
if (mspace != NULL) {
|
|
95 |
mspace->initialize();
|
|
96 |
}
|
|
97 |
return mspace;
|
|
98 |
}
|
|
99 |
|
|
100 |
bool JfrCheckpointManager::initialize() {
|
|
101 |
assert(_free_list_mspace == NULL, "invariant");
|
|
102 |
_free_list_mspace = create_mspace(checkpoint_buffer_size, unlimited_mspace_size, checkpoint_buffer_cache_count, this);
|
|
103 |
if (_free_list_mspace == NULL) {
|
|
104 |
return false;
|
|
105 |
}
|
|
106 |
assert(_epoch_transition_mspace == NULL, "invariant");
|
|
107 |
_epoch_transition_mspace = create_mspace(checkpoint_buffer_size, unlimited_mspace_size, checkpoint_buffer_cache_count, this);
|
|
108 |
if (_epoch_transition_mspace == NULL) {
|
|
109 |
return false;
|
|
110 |
}
|
|
111 |
assert(_type_manager == NULL, "invariant");
|
|
112 |
_type_manager = new JfrTypeManager();
|
|
113 |
if (_type_manager == NULL || !_type_manager->initialize()) {
|
|
114 |
return false;
|
|
115 |
}
|
|
116 |
assert(_lock == NULL, "invariant");
|
|
117 |
_lock = new Mutex(Monitor::leaf - 1, "Checkpoint mutex", Mutex::_allow_vm_block_flag, Monitor::_safepoint_check_never);
|
|
118 |
return _lock != NULL;
|
|
119 |
}
|
|
120 |
|
|
121 |
bool JfrCheckpointManager::use_epoch_transition_mspace(const Thread* thread) const {
|
|
122 |
return _service_thread != thread && OrderAccess::load_acquire(&_checkpoint_epoch_state) != JfrTraceIdEpoch::epoch();
|
|
123 |
}
|
|
124 |
|
|
125 |
void JfrCheckpointManager::synchronize_epoch() {
|
|
126 |
assert(_checkpoint_epoch_state != JfrTraceIdEpoch::epoch(), "invariant");
|
|
127 |
OrderAccess::storestore();
|
|
128 |
_checkpoint_epoch_state = JfrTraceIdEpoch::epoch();
|
|
129 |
}
|
|
130 |
|
|
131 |
void JfrCheckpointManager::shift_epoch() {
|
|
132 |
debug_only(const u1 current_epoch = JfrTraceIdEpoch::current();)
|
|
133 |
JfrTraceIdEpoch::shift_epoch();
|
|
134 |
assert(current_epoch != JfrTraceIdEpoch::current(), "invariant");
|
|
135 |
}
|
|
136 |
|
|
137 |
void JfrCheckpointManager::register_service_thread(const Thread* thread) {
|
|
138 |
_service_thread = thread;
|
|
139 |
}
|
|
140 |
|
|
141 |
void JfrCheckpointManager::register_full(BufferPtr t, Thread* thread) {
|
|
142 |
// nothing here at the moment
|
|
143 |
assert(t->retired(), "invariant");
|
|
144 |
}
|
|
145 |
|
|
146 |
void JfrCheckpointManager::lock() {
|
|
147 |
assert(!_lock->owned_by_self(), "invariant");
|
|
148 |
_lock->lock_without_safepoint_check();
|
|
149 |
}
|
|
150 |
|
|
151 |
void JfrCheckpointManager::unlock() {
|
|
152 |
_lock->unlock();
|
|
153 |
}
|
|
154 |
|
|
155 |
#ifdef ASSERT
|
|
156 |
|
|
157 |
bool JfrCheckpointManager::is_locked() const {
|
|
158 |
return _lock->owned_by_self();
|
|
159 |
}
|
|
160 |
|
|
161 |
static void assert_free_lease(const BufferPtr buffer) {
|
|
162 |
assert(buffer != NULL, "invariant");
|
|
163 |
assert(buffer->acquired_by_self(), "invariant");
|
|
164 |
assert(buffer->lease(), "invariant");
|
|
165 |
}
|
|
166 |
|
|
167 |
static void assert_release(const BufferPtr buffer) {
|
|
168 |
assert(buffer != NULL, "invariant");
|
|
169 |
assert(buffer->lease(), "invariant");
|
|
170 |
assert(buffer->acquired_by_self(), "invariant");
|
|
171 |
}
|
|
172 |
|
|
173 |
#endif // ASSERT
|
|
174 |
|
|
175 |
static BufferPtr lease_free(size_t size, JfrCheckpointMspace* mspace, size_t retry_count, Thread* thread) {
|
|
176 |
static const size_t max_elem_size = mspace->min_elem_size(); // min is max
|
|
177 |
BufferPtr buffer;
|
|
178 |
if (size <= max_elem_size) {
|
|
179 |
BufferPtr buffer = mspace_get_free_lease_with_retry(size, mspace, retry_count, thread);
|
|
180 |
if (buffer != NULL) {
|
|
181 |
DEBUG_ONLY(assert_free_lease(buffer);)
|
|
182 |
return buffer;
|
|
183 |
}
|
|
184 |
}
|
|
185 |
buffer = mspace_allocate_transient_lease_to_free(size, mspace, thread);
|
|
186 |
DEBUG_ONLY(assert_free_lease(buffer);)
|
|
187 |
return buffer;
|
|
188 |
}
|
|
189 |
|
|
190 |
static const size_t lease_retry = 10;
|
|
191 |
|
|
192 |
BufferPtr JfrCheckpointManager::lease_buffer(Thread* thread, size_t size /* 0 */) {
|
|
193 |
JfrCheckpointManager& manager = instance();
|
|
194 |
if (manager.use_epoch_transition_mspace(thread)) {
|
|
195 |
return lease_free(size, manager._epoch_transition_mspace, lease_retry, thread);
|
|
196 |
}
|
|
197 |
return lease_free(size, manager._free_list_mspace, lease_retry, thread);
|
|
198 |
}
|
|
199 |
|
|
200 |
/*
|
|
201 |
* If the buffer was a "lease" from the free list, release back.
|
|
202 |
*
|
|
203 |
* The buffer is effectively invalidated for the thread post-return,
|
|
204 |
* and the caller should take means to ensure that it is not referenced.
|
|
205 |
*/
|
|
206 |
static void release(BufferPtr const buffer, Thread* thread) {
|
|
207 |
DEBUG_ONLY(assert_release(buffer);)
|
|
208 |
buffer->clear_lease();
|
|
209 |
buffer->release();
|
|
210 |
}
|
|
211 |
|
|
212 |
BufferPtr JfrCheckpointManager::flush(BufferPtr old, size_t used, size_t requested, Thread* thread) {
|
|
213 |
assert(old != NULL, "invariant");
|
|
214 |
assert(old->lease(), "invariant");
|
|
215 |
if (0 == requested) {
|
|
216 |
// indicates a lease is being returned
|
|
217 |
release(old, thread);
|
|
218 |
return NULL;
|
|
219 |
}
|
|
220 |
// migration of in-flight information
|
|
221 |
BufferPtr const new_buffer = lease_buffer(thread, used + requested);
|
|
222 |
if (new_buffer != NULL) {
|
|
223 |
migrate_outstanding_writes(old, new_buffer, used, requested);
|
|
224 |
}
|
|
225 |
release(old, thread);
|
|
226 |
return new_buffer; // might be NULL
|
|
227 |
}
|
|
228 |
|
|
229 |
// offsets into the JfrCheckpointEntry
|
|
230 |
static const juint starttime_offset = sizeof(jlong);
|
|
231 |
static const juint duration_offset = starttime_offset + sizeof(jlong);
|
|
232 |
static const juint flushpoint_offset = duration_offset + sizeof(jlong);
|
|
233 |
static const juint types_offset = flushpoint_offset + sizeof(juint);
|
|
234 |
static const juint payload_offset = types_offset + sizeof(juint);
|
|
235 |
|
|
236 |
template <typename Return>
|
|
237 |
static Return read_data(const u1* data) {
|
|
238 |
return JfrBigEndian::read<Return>(data);
|
|
239 |
}
|
|
240 |
|
|
241 |
static jlong total_size(const u1* data) {
|
|
242 |
return read_data<jlong>(data);
|
|
243 |
}
|
|
244 |
|
|
245 |
static jlong starttime(const u1* data) {
|
|
246 |
return read_data<jlong>(data + starttime_offset);
|
|
247 |
}
|
|
248 |
|
|
249 |
static jlong duration(const u1* data) {
|
|
250 |
return read_data<jlong>(data + duration_offset);
|
|
251 |
}
|
|
252 |
|
|
253 |
static bool is_flushpoint(const u1* data) {
|
|
254 |
return read_data<juint>(data + flushpoint_offset) == (juint)1;
|
|
255 |
}
|
|
256 |
|
|
257 |
static juint number_of_types(const u1* data) {
|
|
258 |
return read_data<juint>(data + types_offset);
|
|
259 |
}
|
|
260 |
|
|
261 |
static void write_checkpoint_header(JfrChunkWriter& cw, intptr_t offset_prev_cp_event, const u1* data) {
|
|
262 |
cw.reserve(sizeof(u4));
|
|
263 |
cw.write((u8)EVENT_CHECKPOINT);
|
|
264 |
cw.write(starttime(data));
|
|
265 |
cw.write(duration(data));
|
|
266 |
cw.write((jlong)offset_prev_cp_event);
|
|
267 |
cw.write(is_flushpoint(data));
|
|
268 |
cw.write(number_of_types(data));
|
|
269 |
}
|
|
270 |
|
|
271 |
static void write_checkpoint_content(JfrChunkWriter& cw, const u1* data, size_t size) {
|
|
272 |
assert(data != NULL, "invariant");
|
|
273 |
cw.write_unbuffered(data + payload_offset, size);
|
|
274 |
}
|
|
275 |
|
|
276 |
static size_t write_checkpoint_event(JfrChunkWriter& cw, const u1* data) {
|
|
277 |
assert(data != NULL, "invariant");
|
|
278 |
const intptr_t previous_checkpoint_event = cw.previous_checkpoint_offset();
|
|
279 |
const intptr_t event_begin = cw.current_offset();
|
|
280 |
const intptr_t offset_to_previous_checkpoint_event = 0 == previous_checkpoint_event ? 0 : previous_checkpoint_event - event_begin;
|
|
281 |
const jlong total_checkpoint_size = total_size(data);
|
|
282 |
write_checkpoint_header(cw, offset_to_previous_checkpoint_event, data);
|
|
283 |
write_checkpoint_content(cw, data, total_checkpoint_size - sizeof(JfrCheckpointEntry));
|
|
284 |
const jlong checkpoint_event_size = cw.current_offset() - event_begin;
|
|
285 |
cw.write_padded_at_offset<u4>(checkpoint_event_size, event_begin);
|
|
286 |
cw.set_previous_checkpoint_offset(event_begin);
|
|
287 |
return (size_t)total_checkpoint_size;
|
|
288 |
}
|
|
289 |
|
|
290 |
static size_t write_checkpoints(JfrChunkWriter& cw, const u1* data, size_t size) {
|
|
291 |
assert(cw.is_valid(), "invariant");
|
|
292 |
assert(data != NULL, "invariant");
|
|
293 |
assert(size > 0, "invariant");
|
|
294 |
const u1* const limit = data + size;
|
|
295 |
const u1* next_entry = data;
|
|
296 |
size_t processed = 0;
|
|
297 |
while (next_entry < limit) {
|
|
298 |
const size_t checkpoint_size = write_checkpoint_event(cw, next_entry);
|
|
299 |
processed += checkpoint_size;
|
|
300 |
next_entry += checkpoint_size;
|
|
301 |
}
|
|
302 |
assert(next_entry == limit, "invariant");
|
|
303 |
return processed;
|
|
304 |
}
|
|
305 |
|
|
306 |
template <typename T>
|
|
307 |
class CheckpointWriteOp {
|
|
308 |
private:
|
|
309 |
JfrChunkWriter& _writer;
|
|
310 |
size_t _processed;
|
|
311 |
public:
|
|
312 |
typedef T Type;
|
|
313 |
CheckpointWriteOp(JfrChunkWriter& writer) : _writer(writer), _processed(0) {}
|
|
314 |
bool write(Type* t, const u1* data, size_t size) {
|
|
315 |
_processed += write_checkpoints(_writer, data, size);
|
|
316 |
return true;
|
|
317 |
}
|
|
318 |
size_t processed() const { return _processed; }
|
|
319 |
};
|
|
320 |
|
|
321 |
typedef CheckpointWriteOp<JfrCheckpointMspace::Type> WriteOperation;
|
|
322 |
typedef MutexedWriteOp<WriteOperation> MutexedWriteOperation;
|
|
323 |
typedef ReleaseOp<JfrCheckpointMspace> CheckpointReleaseOperation;
|
|
324 |
typedef CompositeOperation<MutexedWriteOperation, CheckpointReleaseOperation> CheckpointWriteOperation;
|
|
325 |
|
|
326 |
static size_t write_mspace_exclusive(JfrCheckpointMspace* mspace, JfrChunkWriter& chunkwriter) {
|
|
327 |
Thread* const thread = Thread::current();
|
|
328 |
WriteOperation wo(chunkwriter);
|
|
329 |
MutexedWriteOperation mwo(wo);
|
|
330 |
CheckpointReleaseOperation cro(mspace, thread, false);
|
|
331 |
CheckpointWriteOperation cpwo(&mwo, &cro);
|
|
332 |
assert(mspace->is_full_empty(), "invariant");
|
|
333 |
process_free_list(cpwo, mspace);
|
|
334 |
return wo.processed();
|
|
335 |
}
|
|
336 |
|
|
337 |
size_t JfrCheckpointManager::write() {
|
|
338 |
const size_t processed = write_mspace_exclusive(_free_list_mspace, _chunkwriter);
|
|
339 |
synchronize_epoch();
|
|
340 |
return processed;
|
|
341 |
}
|
|
342 |
|
|
343 |
size_t JfrCheckpointManager::write_epoch_transition_mspace() {
|
|
344 |
return write_mspace_exclusive(_epoch_transition_mspace, _chunkwriter);
|
|
345 |
}
|
|
346 |
|
|
347 |
typedef DiscardOp<DefaultDiscarder<JfrBuffer> > DiscardOperation;
|
|
348 |
size_t JfrCheckpointManager::clear() {
|
|
349 |
DiscardOperation discarder(mutexed); // mutexed discard mode
|
|
350 |
process_free_list(discarder, _free_list_mspace);
|
|
351 |
process_free_list(discarder, _epoch_transition_mspace);
|
|
352 |
synchronize_epoch();
|
|
353 |
return discarder.processed();
|
|
354 |
}
|
|
355 |
|
|
356 |
bool JfrCheckpointManager::register_serializer(JfrTypeId id, bool require_safepoint, bool permit_cache, JfrSerializer* cs) {
|
|
357 |
assert(cs != NULL, "invariant");
|
|
358 |
return instance()._type_manager->register_serializer(id, require_safepoint, permit_cache, cs);
|
|
359 |
}
|
|
360 |
|
|
361 |
size_t JfrCheckpointManager::write_types() {
|
|
362 |
JfrCheckpointWriter writer(false, true, Thread::current());
|
|
363 |
_type_manager->write_types(writer);
|
|
364 |
return writer.used_size();
|
|
365 |
}
|
|
366 |
|
|
367 |
size_t JfrCheckpointManager::write_safepoint_types() {
|
|
368 |
// this is also a "flushpoint"
|
|
369 |
JfrCheckpointWriter writer(true, true, Thread::current());
|
|
370 |
_type_manager->write_safepoint_types(writer);
|
|
371 |
return writer.used_size();
|
|
372 |
}
|
|
373 |
|
|
374 |
void JfrCheckpointManager::write_type_set() {
|
|
375 |
_type_manager->write_type_set();
|
|
376 |
}
|
|
377 |
|
|
378 |
void JfrCheckpointManager::write_type_set_for_unloaded_classes() {
|
|
379 |
assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");
|
|
380 |
instance()._type_manager->write_type_set_for_unloaded_classes();
|
|
381 |
}
|
|
382 |
|
|
383 |
void JfrCheckpointManager::create_thread_checkpoint(JavaThread* jt) {
|
|
384 |
instance()._type_manager->create_thread_checkpoint(jt);
|
|
385 |
}
|
|
386 |
|
|
387 |
void JfrCheckpointManager::write_thread_checkpoint(JavaThread* jt) {
|
|
388 |
instance()._type_manager->write_thread_checkpoint(jt);
|
|
389 |
}
|