0 /*                                                                                                                                   
1  * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.                                                      
2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.                                                                     
3  *                                                                                                                                   
4  * This code is free software; you can redistribute it and/or modify it                                                              
5  * under the terms of the GNU General Public License version 2 only, as                                                              
6  * published by the Free Software Foundation.                                                                                        
7  *                                                                                                                                   
8  * This code is distributed in the hope that it will be useful, but WITHOUT                                                          
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or                                                             
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License                                                             
11  * version 2 for more details (a copy is included in the LICENSE file that                                                           
12  * accompanied this code).                                                                                                           
13  *                                                                                                                                   
14  * You should have received a copy of the GNU General Public License version                                                         
15  * 2 along with this work; if not, write to the Free Software Foundation,                                                            
16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.                                                                     
17  *                                                                                                                                   
18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA                                                           
19  * or visit www.oracle.com if you need additional information or have any                                                            
20  * questions.                                                                                                                        
21  *                                                                                                                                   
22  */                                                                                                                                  
23 
24 #include "precompiled.hpp"                                                                                                           
25 #include "classfile/javaClasses.inline.hpp"                                                                                          
26 #include "jfr/recorder/jfrRecorder.hpp"                                                                                              
27 #include "jfr/recorder/checkpoint/jfrCheckpointManager.hpp"                                                                          
28 #include "jfr/recorder/checkpoint/jfrCheckpointWriter.hpp"                                                                           
29 #include "jfr/recorder/checkpoint/types/jfrTypeManager.hpp"                                                                          
30 #include "jfr/recorder/checkpoint/types/traceid/jfrTraceIdEpoch.hpp"                                                                 
31 #include "jfr/recorder/service/jfrOptionSet.hpp"                                                                                     
32 #include "jfr/recorder/storage/jfrMemorySpace.inline.hpp"                                                                            
33 #include "jfr/recorder/storage/jfrStorageUtils.inline.hpp"                                                                           
34 #include "jfr/recorder/repository/jfrChunkWriter.hpp"                                                                                
35 #include "jfr/utilities/jfrBigEndian.hpp"                                                                                            
36 #include "jfr/utilities/jfrTypes.hpp"                                                                                                
37 #include "logging/log.hpp"                                                                                                           
38 #include "memory/resourceArea.hpp"                                                                                                   
39 #include "runtime/mutexLocker.hpp"                                                                                                   
40 #include "runtime/orderAccess.hpp"                                                                                                   
41 #include "runtime/os.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   _service_thread(NULL),                                                                                                             
69   _chunkwriter(cw),                                                                                                                  
70   _checkpoint_epoch_state(JfrTraceIdEpoch::epoch()) {}                                                                               
71 
72 JfrCheckpointManager::~JfrCheckpointManager() {                                                                                      
73   if (_free_list_mspace != NULL) {                                                                                                   
74     delete _free_list_mspace;                                                                                                        
75   }                                                                                                                                  
76   if (_epoch_transition_mspace != NULL) {                                                                                            
77     delete _epoch_transition_mspace;                                                                                                 
78   }                                                                                                                                  
79   if (_lock != NULL) {                                                                                                               
80     delete _lock;                                                                                                                    
81   }                                                                                                                                  
82   JfrTypeManager::clear();                                                                                                           
83 }                                                                                                                                    
84 
85 static const size_t unlimited_mspace_size = 0;                                                                                       
86 static const size_t checkpoint_buffer_cache_count = 2;                                                                               
87 static const size_t checkpoint_buffer_size = 512 * K;                                                                                
88 
89 static JfrCheckpointMspace* create_mspace(size_t buffer_size, size_t limit, size_t cache_count, JfrCheckpointManager* system) {      
90   JfrCheckpointMspace* mspace = new JfrCheckpointMspace(buffer_size, limit, cache_count, system);                                    
91   if (mspace != NULL) {                                                                                                              
92     mspace->initialize();                                                                                                            
93   }                                                                                                                                  
94   return mspace;                                                                                                                     
95 }                                                                                                                                    
96 
97 bool JfrCheckpointManager::initialize() {                                                                                            
98   assert(_free_list_mspace == NULL, "invariant");                                                                                    
99   _free_list_mspace = create_mspace(checkpoint_buffer_size, unlimited_mspace_size, checkpoint_buffer_cache_count, this);             
100   if (_free_list_mspace == NULL) {                                                                                                   
101     return false;                                                                                                                    
102   }                                                                                                                                  
103   assert(_epoch_transition_mspace == NULL, "invariant");                                                                             
104   _epoch_transition_mspace = create_mspace(checkpoint_buffer_size, unlimited_mspace_size, checkpoint_buffer_cache_count, this);      
105   if (_epoch_transition_mspace == NULL) {                                                                                            
106     return false;                                                                                                                    
107   }                                                                                                                                  
108   assert(_lock == NULL, "invariant");                                                                                                
109   _lock = new Mutex(Monitor::leaf - 1, "Checkpoint mutex", Mutex::_allow_vm_block_flag, Monitor::_safepoint_check_never);            
110   if (_lock == NULL) {                                                                                                               
111     return false;                                                                                                                    
112   }                                                                                                                                  
113   return JfrTypeManager::initialize();                                                                                               
114 }                                                                                                                                    
115 
116 bool JfrCheckpointManager::use_epoch_transition_mspace(const Thread* thread) const {                                                 
117   return _service_thread != thread && OrderAccess::load_acquire(&_checkpoint_epoch_state) != JfrTraceIdEpoch::epoch();               
118 }                                                                                                                                    
119 
120 void JfrCheckpointManager::synchronize_epoch() {                                                                                     
121   assert(_checkpoint_epoch_state != JfrTraceIdEpoch::epoch(), "invariant");                                                          
122   OrderAccess::storestore();                                                                                                         
123   _checkpoint_epoch_state = JfrTraceIdEpoch::epoch();                                                                                
124 }                                                                                                                                    
125 
126 void JfrCheckpointManager::shift_epoch() {                                                                                           
127   debug_only(const u1 current_epoch = JfrTraceIdEpoch::current();)                                                                   
128   JfrTraceIdEpoch::shift_epoch();                                                                                                    
129   assert(current_epoch != JfrTraceIdEpoch::current(), "invariant");                                                                  
130 }                                                                                                                                    
131 
132 void JfrCheckpointManager::register_service_thread(const Thread* thread) {                                                           
133   _service_thread = thread;                                                                                                          
134 }                                                                                                                                    
135 
136 void JfrCheckpointManager::register_full(BufferPtr t, Thread* thread) {                                                              
137   // nothing here at the moment                                                                                                      
138   assert(t->retired(), "invariant");                                                                                                 
139 }                                                                                                                                    
140 
141 void JfrCheckpointManager::lock() {                                                                                                  
142   assert(!_lock->owned_by_self(), "invariant");                                                                                      
143   _lock->lock_without_safepoint_check();                                                                                             
144 }                                                                                                                                    
145 
146 void JfrCheckpointManager::unlock() {                                                                                                
147   _lock->unlock();                                                                                                                   
148 }                                                                                                                                    
149 
150 #ifdef ASSERT                                                                                                                        
151 
152 bool JfrCheckpointManager::is_locked() const {                                                                                       
153   return _lock->owned_by_self();                                                                                                     
154 }                                                                                                                                    
155 
156 static void assert_free_lease(const BufferPtr buffer) {                                                                              
157   assert(buffer != NULL, "invariant");                                                                                               
158   assert(buffer->acquired_by_self(), "invariant");                                                                                   
159   assert(buffer->lease(), "invariant");                                                                                              
160 }                                                                                                                                    
161 
162 static void assert_release(const BufferPtr buffer) {                                                                                 
163   assert(buffer != NULL, "invariant");                                                                                               
164   assert(buffer->lease(), "invariant");                                                                                              
165   assert(buffer->acquired_by_self(), "invariant");                                                                                   
166 }                                                                                                                                    
167 
168 #endif // ASSERT                                                                                                                     
169 
170 static BufferPtr lease_free(size_t size, JfrCheckpointMspace* mspace, size_t retry_count, Thread* thread) {                          
171   static const size_t max_elem_size = mspace->min_elem_size(); // min is max                                                         
172   BufferPtr buffer;                                                                                                                  
173   if (size <= max_elem_size) {                                                                                                       
174     BufferPtr buffer = mspace_get_free_lease_with_retry(size, mspace, retry_count, thread);                                          
175     if (buffer != NULL) {                                                                                                            
176       DEBUG_ONLY(assert_free_lease(buffer);)                                                                                         
177       return buffer;                                                                                                                 
178     }                                                                                                                                
179   }                                                                                                                                  
180   buffer = mspace_allocate_transient_lease_to_free(size, mspace, thread);                                                            
181   DEBUG_ONLY(assert_free_lease(buffer);)                                                                                             
182   return buffer;                                                                                                                     
183 }                                                                                                                                    
184 
185 static const size_t lease_retry = 10;                                                                                                
186 
187 BufferPtr JfrCheckpointManager::lease_buffer(Thread* thread, size_t size /* 0 */) {                                                  
188   JfrCheckpointManager& manager = instance();                                                                                        
189   if (manager.use_epoch_transition_mspace(thread)) {                                                                                 
190     return lease_free(size, manager._epoch_transition_mspace, lease_retry, thread);                                                  
191   }                                                                                                                                  
192   return lease_free(size, manager._free_list_mspace, lease_retry, thread);                                                           
193 }                                                                                                                                    
194 
195 /*                                                                                                                                   
196 * If the buffer was a "lease" from the free list, release back.                                                                      
197 *                                                                                                                                    
198 * The buffer is effectively invalidated for the thread post-return,                                                                  
199 * and the caller should take means to ensure that it is not referenced.                                                              
200 */                                                                                                                                   
201 static void release(BufferPtr const buffer, Thread* thread) {                                                                        
202   DEBUG_ONLY(assert_release(buffer);)                                                                                                
203   buffer->clear_lease();                                                                                                             
204   buffer->release();                                                                                                                 
205 }                                                                                                                                    
206 
207 BufferPtr JfrCheckpointManager::flush(BufferPtr old, size_t used, size_t requested, Thread* thread) {                                
208   assert(old != NULL, "invariant");                                                                                                  
209   assert(old->lease(), "invariant");                                                                                                 
210   if (0 == requested) {                                                                                                              
211     // indicates a lease is being returned                                                                                           
212     release(old, thread);                                                                                                            
213     return NULL;                                                                                                                     
214   }                                                                                                                                  
215   // migration of in-flight information                                                                                              
216   BufferPtr const new_buffer = lease_buffer(thread, used + requested);                                                               
217   if (new_buffer != NULL) {                                                                                                          
218     migrate_outstanding_writes(old, new_buffer, used, requested);                                                                    
219   }                                                                                                                                  
220   release(old, thread);                                                                                                              
221   return new_buffer; // might be NULL                                                                                                
222 }                                                                                                                                    
223 
224 // offsets into the JfrCheckpointEntry                                                                                               
225 static const juint starttime_offset = sizeof(jlong);                                                                                 
226 static const juint duration_offset = starttime_offset + sizeof(jlong);                                                               
227 static const juint flushpoint_offset = duration_offset + sizeof(jlong);                                                              
228 static const juint types_offset = flushpoint_offset + sizeof(juint);                                                                 
229 static const juint payload_offset = types_offset + sizeof(juint);                                                                    
230 
231 template <typename Return>                                                                                                           
232 static Return read_data(const u1* data) {                                                                                            
233   return JfrBigEndian::read<Return>(data);                                                                                           
234 }                                                                                                                                    
235 
236 static jlong total_size(const u1* data) {                                                                                            
237   return read_data<jlong>(data);                                                                                                     
238 }                                                                                                                                    
239 
240 static jlong starttime(const u1* data) {                                                                                             
241   return read_data<jlong>(data + starttime_offset);                                                                                  
242 }                                                                                                                                    
243 
244 static jlong duration(const u1* data) {                                                                                              
245   return read_data<jlong>(data + duration_offset);                                                                                   
246 }                                                                                                                                    
247 
248 static bool is_flushpoint(const u1* data) {                                                                                          
249   return read_data<juint>(data + flushpoint_offset) == (juint)1;                                                                     
250 }                                                                                                                                    
251 
252 static juint number_of_types(const u1* data) {                                                                                       
253   return read_data<juint>(data + types_offset);                                                                                      
254 }                                                                                                                                    
255 
256 static void write_checkpoint_header(JfrChunkWriter& cw, intptr_t offset_prev_cp_event, const u1* data) {                             
257   cw.reserve(sizeof(u4));                                                                                                            
258   cw.write((u8)EVENT_CHECKPOINT);                                                                                                    
259   cw.write(starttime(data));                                                                                                         
260   cw.write(duration(data));                                                                                                          
261   cw.write((jlong)offset_prev_cp_event);                                                                                             
262   cw.write(is_flushpoint(data));                                                                                                     
263   cw.write(number_of_types(data));                                                                                                   
264 }                                                                                                                                    
265 
266 static void write_checkpoint_content(JfrChunkWriter& cw, const u1* data, size_t size) {                                              
267   assert(data != NULL, "invariant");                                                                                                 
268   cw.write_unbuffered(data + payload_offset, size);                                                                                  
269 }                                                                                                                                    
270 
271 static size_t write_checkpoint_event(JfrChunkWriter& cw, const u1* data) {                                                           
272   assert(data != NULL, "invariant");                                                                                                 
273   const intptr_t previous_checkpoint_event = cw.previous_checkpoint_offset();                                                        
274   const intptr_t event_begin = cw.current_offset();                                                                                  
275   const intptr_t offset_to_previous_checkpoint_event = 0 == previous_checkpoint_event ? 0 : previous_checkpoint_event - event_begin; 
276   const jlong total_checkpoint_size = total_size(data);                                                                              
277   write_checkpoint_header(cw, offset_to_previous_checkpoint_event, data);                                                            
278   write_checkpoint_content(cw, data, total_checkpoint_size - sizeof(JfrCheckpointEntry));                                            
279   const jlong checkpoint_event_size = cw.current_offset() - event_begin;                                                             
280   cw.write_padded_at_offset<u4>(checkpoint_event_size, event_begin);                                                                 
281   cw.set_previous_checkpoint_offset(event_begin);                                                                                    
282   return (size_t)total_checkpoint_size;                                                                                              
283 }                                                                                                                                    
284 
285 static size_t write_checkpoints(JfrChunkWriter& cw, const u1* data, size_t size) {                                                   
286   assert(cw.is_valid(), "invariant");                                                                                                
287   assert(data != NULL, "invariant");                                                                                                 
288   assert(size > 0, "invariant");                                                                                                     
289   const u1* const limit = data + size;                                                                                               
290   const u1* next_entry = data;                                                                                                       
291   size_t processed = 0;                                                                                                              
292   while (next_entry < limit) {                                                                                                       
293     const size_t checkpoint_size = write_checkpoint_event(cw, next_entry);                                                           
294     processed += checkpoint_size;                                                                                                    
295     next_entry += checkpoint_size;                                                                                                   
296   }                                                                                                                                  
297   assert(next_entry == limit, "invariant");                                                                                          
298   return processed;                                                                                                                  
299 }                                                                                                                                    
300 
301 template <typename T>                                                                                                                
302 class CheckpointWriteOp {                                                                                                            
303  private:                                                                                                                            
304   JfrChunkWriter& _writer;                                                                                                           
305   size_t _processed;                                                                                                                 
306  public:                                                                                                                             
307   typedef T Type;                                                                                                                    
308   CheckpointWriteOp(JfrChunkWriter& writer) : _writer(writer), _processed(0) {}                                                      
309   bool write(Type* t, const u1* data, size_t size) {                                                                                 
310     _processed += write_checkpoints(_writer, data, size);                                                                            
311     return true;                                                                                                                     
312   }                                                                                                                                  
313   size_t processed() const { return _processed; }                                                                                    
314 };                                                                                                                                   
315 
316 typedef CheckpointWriteOp<JfrCheckpointMspace::Type> WriteOperation;                                                                 
317 typedef MutexedWriteOp<WriteOperation> MutexedWriteOperation;                                                                        
318 typedef ReleaseOp<JfrCheckpointMspace> CheckpointReleaseOperation;                                                                   
319 typedef CompositeOperation<MutexedWriteOperation, CheckpointReleaseOperation> CheckpointWriteOperation;                              
320 
321 static size_t write_mspace_exclusive(JfrCheckpointMspace* mspace, JfrChunkWriter& chunkwriter) {                                     
322   Thread* const thread = Thread::current();                                                                                          
323   WriteOperation wo(chunkwriter);                                                                                                    
324   MutexedWriteOperation mwo(wo);                                                                                                     
325   CheckpointReleaseOperation cro(mspace, thread, false);                                                                             
326   CheckpointWriteOperation cpwo(&mwo, &cro);                                                                                         
327   assert(mspace->is_full_empty(), "invariant");                                                                                      
328   process_free_list(cpwo, mspace);                                                                                                   
329   return wo.processed();                                                                                                             
330 }                                                                                                                                    
331 
332 size_t JfrCheckpointManager::write() {                                                                                               
333   const size_t processed = write_mspace_exclusive(_free_list_mspace, _chunkwriter);                                                  
334   synchronize_epoch();                                                                                                               
335   return processed;                                                                                                                  
336 }                                                                                                                                    
337 
338 size_t JfrCheckpointManager::write_epoch_transition_mspace() {                                                                       
339   return write_mspace_exclusive(_epoch_transition_mspace, _chunkwriter);                                                             
340 }                                                                                                                                    
341 
342 typedef DiscardOp<DefaultDiscarder<JfrBuffer> > DiscardOperation;                                                                    
343 size_t JfrCheckpointManager::clear() {                                                                                               
344   DiscardOperation discarder(mutexed); // mutexed discard mode                                                                       
345   process_free_list(discarder, _free_list_mspace);                                                                                   
346   process_free_list(discarder, _epoch_transition_mspace);                                                                            
347   synchronize_epoch();                                                                                                               
348   return discarder.processed();                                                                                                      
349 }                                                                                                                                    
350 
351 size_t JfrCheckpointManager::write_types() {                                                                                         
352   JfrCheckpointWriter writer(false, true, Thread::current());                                                                        
353   JfrTypeManager::write_types(writer);                                                                                               
354   return writer.used_size();                                                                                                         
355 }                                                                                                                                    
356 
357 size_t JfrCheckpointManager::write_safepoint_types() {                                                                               
358   // this is also a "flushpoint"                                                                                                     
359   JfrCheckpointWriter writer(true, true, Thread::current());                                                                         
360   JfrTypeManager::write_safepoint_types(writer);                                                                                     
361   return writer.used_size();                                                                                                         
362 }                                                                                                                                    
363 
364 void JfrCheckpointManager::write_type_set() {                                                                                        
365   JfrTypeManager::write_type_set();                                                                                                  
366 }                                                                                                                                    
367 
368 void JfrCheckpointManager::write_type_set_for_unloaded_classes() {                                                                   
369   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint!");                                                          
370   JfrTypeManager::write_type_set_for_unloaded_classes();                                                                             
371 }                                                                                                                                    
372 
373 void JfrCheckpointManager::create_thread_checkpoint(JavaThread* jt) {                                                                
374   JfrTypeManager::create_thread_checkpoint(jt);                                                                                      
375 }                                                                                                                                    
376 
377 void JfrCheckpointManager::write_thread_checkpoint(JavaThread* jt) {                                                                 
378   JfrTypeManager::write_thread_checkpoint(jt);                                                                                       
379 }                                                                                                                                    
--- EOF ---