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