116 // enclosing BufferBlobs. |
116 // enclosing BufferBlobs. |
117 address addr = cb->instructions_begin(); |
117 address addr = cb->instructions_begin(); |
118 for (int i=0; i<_global_code_blobs->length(); i++) { |
118 for (int i=0; i<_global_code_blobs->length(); i++) { |
119 JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i); |
119 JvmtiCodeBlobDesc* scb = _global_code_blobs->at(i); |
120 if (addr == scb->code_begin()) { |
120 if (addr == scb->code_begin()) { |
|
121 ShouldNotReachHere(); |
121 return; |
122 return; |
122 } |
123 } |
123 } |
124 } |
124 |
125 |
125 // we must name the CodeBlob - some CodeBlobs already have names :- |
|
126 // - stubs used by compiled code to call a (static) C++ runtime routine |
|
127 // - non-relocatable machine code such as the interpreter, stubroutines, etc. |
|
128 // - various singleton blobs |
|
129 // |
|
130 // others are unnamed so we create a name :- |
|
131 // - OSR adapter (interpreter frame that has been on-stack replaced) |
|
132 // - I2C and C2I adapters |
|
133 const char* name = NULL; |
|
134 if (cb->is_runtime_stub()) { |
|
135 name = ((RuntimeStub*)cb)->name(); |
|
136 } |
|
137 if (cb->is_buffer_blob()) { |
|
138 name = ((BufferBlob*)cb)->name(); |
|
139 } |
|
140 if (cb->is_deoptimization_stub() || cb->is_safepoint_stub()) { |
|
141 name = ((SingletonBlob*)cb)->name(); |
|
142 } |
|
143 if (cb->is_uncommon_trap_stub() || cb->is_exception_stub()) { |
|
144 name = ((SingletonBlob*)cb)->name(); |
|
145 } |
|
146 |
|
147 // record the CodeBlob details as a JvmtiCodeBlobDesc |
126 // record the CodeBlob details as a JvmtiCodeBlobDesc |
148 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(name, cb->instructions_begin(), |
127 JvmtiCodeBlobDesc* scb = new JvmtiCodeBlobDesc(cb->name(), cb->instructions_begin(), |
149 cb->instructions_end()); |
128 cb->instructions_end()); |
150 _global_code_blobs->append(scb); |
129 _global_code_blobs->append(scb); |
151 } |
130 } |
152 |
131 |
153 |
132 |
211 } |
193 } |
212 return JVMTI_ERROR_NONE; |
194 return JVMTI_ERROR_NONE; |
213 } |
195 } |
214 |
196 |
215 |
197 |
216 // Support class to describe a nmethod in the CodeCache |
|
217 |
|
218 class nmethodDesc: public CHeapObj { |
|
219 private: |
|
220 jmethodID _jmethod_id; |
|
221 address _code_begin; |
|
222 address _code_end; |
|
223 jvmtiAddrLocationMap* _map; |
|
224 jint _map_length; |
|
225 public: |
|
226 nmethodDesc(jmethodID jmethod_id, address code_begin, address code_end, |
|
227 jvmtiAddrLocationMap* map, jint map_length) { |
|
228 _jmethod_id = jmethod_id; |
|
229 _code_begin = code_begin; |
|
230 _code_end = code_end; |
|
231 _map = map; |
|
232 _map_length = map_length; |
|
233 } |
|
234 jmethodID jmethod_id() const { return _jmethod_id; } |
|
235 address code_begin() const { return _code_begin; } |
|
236 address code_end() const { return _code_end; } |
|
237 jvmtiAddrLocationMap* map() const { return _map; } |
|
238 jint map_length() const { return _map_length; } |
|
239 }; |
|
240 |
|
241 |
|
242 // Support class to collect a list of the nmethod CodeBlobs in |
|
243 // the CodeCache. |
|
244 // |
|
245 // Usage :- |
|
246 // |
|
247 // nmethodCollector collector; |
|
248 // |
|
249 // collector.collect(); |
|
250 // JvmtiCodeBlobDesc* blob = collector.first(); |
|
251 // while (blob != NULL) { |
|
252 // : |
|
253 // blob = collector.next(); |
|
254 // } |
|
255 // |
|
256 class nmethodCollector : StackObj { |
|
257 private: |
|
258 GrowableArray<nmethodDesc*>* _nmethods; // collect nmethods |
|
259 int _pos; // iteration support |
|
260 |
|
261 // used during a collection |
|
262 static GrowableArray<nmethodDesc*>* _global_nmethods; |
|
263 static void do_nmethod(nmethod* nm); |
|
264 public: |
|
265 nmethodCollector() { |
|
266 _nmethods = NULL; |
|
267 _pos = -1; |
|
268 } |
|
269 ~nmethodCollector() { |
|
270 if (_nmethods != NULL) { |
|
271 for (int i=0; i<_nmethods->length(); i++) { |
|
272 nmethodDesc* blob = _nmethods->at(i); |
|
273 if (blob->map()!= NULL) { |
|
274 FREE_C_HEAP_ARRAY(jvmtiAddrLocationMap, blob->map()); |
|
275 } |
|
276 } |
|
277 delete _nmethods; |
|
278 } |
|
279 } |
|
280 |
|
281 // collect list of nmethods in the cache |
|
282 void collect(); |
|
283 |
|
284 // iteration support - return first code blob |
|
285 nmethodDesc* first() { |
|
286 assert(_nmethods != NULL, "not collected"); |
|
287 if (_nmethods->length() == 0) { |
|
288 return NULL; |
|
289 } |
|
290 _pos = 0; |
|
291 return _nmethods->at(0); |
|
292 } |
|
293 |
|
294 // iteration support - return next code blob |
|
295 nmethodDesc* next() { |
|
296 assert(_pos >= 0, "iteration not started"); |
|
297 if (_pos+1 >= _nmethods->length()) { |
|
298 return NULL; |
|
299 } |
|
300 return _nmethods->at(++_pos); |
|
301 } |
|
302 }; |
|
303 |
|
304 // used during collection |
|
305 GrowableArray<nmethodDesc*>* nmethodCollector::_global_nmethods; |
|
306 |
|
307 |
|
308 // called for each nmethod in the CodeCache |
|
309 // |
|
310 // This function simply adds a descriptor for each nmethod to the global list. |
|
311 |
|
312 void nmethodCollector::do_nmethod(nmethod* nm) { |
|
313 // ignore zombies |
|
314 if (!nm->is_alive()) { |
|
315 return; |
|
316 } |
|
317 |
|
318 assert(nm->method() != NULL, "checking"); |
|
319 |
|
320 // create the location map for the nmethod. |
|
321 jvmtiAddrLocationMap* map; |
|
322 jint map_length; |
|
323 JvmtiCodeBlobEvents::build_jvmti_addr_location_map(nm, &map, &map_length); |
|
324 |
|
325 // record the nmethod details |
|
326 nmethodDesc* snm = new nmethodDesc(nm->get_and_cache_jmethod_id(), |
|
327 nm->code_begin(), |
|
328 nm->code_end(), |
|
329 map, |
|
330 map_length); |
|
331 _global_nmethods->append(snm); |
|
332 } |
|
333 |
|
334 // collects a list of nmethod in the CodeCache. |
|
335 // |
|
336 // The created list is growable array of nmethodDesc - each one describes |
|
337 // a nmethod and includs its JVMTI address location map. |
|
338 |
|
339 void nmethodCollector::collect() { |
|
340 assert_locked_or_safepoint(CodeCache_lock); |
|
341 assert(_global_nmethods == NULL, "checking"); |
|
342 |
|
343 // create the list |
|
344 _global_nmethods = new (ResourceObj::C_HEAP) GrowableArray<nmethodDesc*>(100,true); |
|
345 |
|
346 // any a descriptor for each nmethod to the list. |
|
347 CodeCache::nmethods_do(do_nmethod); |
|
348 |
|
349 // make the list the instance list |
|
350 _nmethods = _global_nmethods; |
|
351 _global_nmethods = NULL; |
|
352 } |
|
353 |
|
354 // Generate a COMPILED_METHOD_LOAD event for each nnmethod |
198 // Generate a COMPILED_METHOD_LOAD event for each nnmethod |
355 |
|
356 jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) { |
199 jvmtiError JvmtiCodeBlobEvents::generate_compiled_method_load_events(JvmtiEnv* env) { |
357 HandleMark hm; |
200 HandleMark hm; |
358 nmethodCollector collector; |
201 |
359 |
202 // Walk the CodeCache notifying for live nmethods. The code cache |
360 // first collect all nmethods |
203 // may be changing while this is happening which is ok since newly |
361 { |
204 // created nmethod will notify normally and nmethods which are freed |
362 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); |
205 // can be safely skipped. |
363 collector.collect(); |
206 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); |
364 } |
207 nmethod* current = CodeCache::first_nmethod(); |
365 |
208 while (current != NULL) { |
366 // iterate over the list and post an event for each nmethod |
209 // Lock the nmethod so it can't be freed |
367 nmethodDesc* nm_desc = collector.first(); |
210 nmethodLocker nml(current); |
368 while (nm_desc != NULL) { |
211 |
369 jmethodID mid = nm_desc->jmethod_id(); |
212 // Only notify for live nmethods |
370 assert(mid != NULL, "checking"); |
213 if (current->is_alive()) { |
371 JvmtiExport::post_compiled_method_load(env, mid, |
214 // Don't hold the lock over the notify or jmethodID creation |
372 (jint)(nm_desc->code_end() - nm_desc->code_begin()), |
215 MutexUnlockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag); |
373 nm_desc->code_begin(), nm_desc->map_length(), |
216 current->get_and_cache_jmethod_id(); |
374 nm_desc->map()); |
217 JvmtiExport::post_compiled_method_load(current); |
375 nm_desc = collector.next(); |
218 } |
|
219 current = CodeCache::next_nmethod(current); |
376 } |
220 } |
377 return JVMTI_ERROR_NONE; |
221 return JVMTI_ERROR_NONE; |
378 } |
222 } |
379 |
223 |
380 |
224 |