Print this page
Split |
Close |
Expand all |
Collapse all |
--- old/src/share/vm/code/codeCache.cpp
+++ new/src/share/vm/code/codeCache.cpp
1 1 /*
2 2 * Copyright 1997-2010 Sun Microsystems, Inc. All Rights Reserved.
3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 4 *
5 5 * This code is free software; you can redistribute it and/or modify it
6 6 * under the terms of the GNU General Public License version 2 only, as
7 7 * published by the Free Software Foundation.
8 8 *
9 9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 12 * version 2 for more details (a copy is included in the LICENSE file that
13 13 * accompanied this code).
14 14 *
15 15 * You should have received a copy of the GNU General Public License version
16 16 * 2 along with this work; if not, write to the Free Software Foundation,
17 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 18 *
19 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 21 * have any questions.
22 22 *
23 23 */
24 24
25 25 # include "incls/_precompiled.incl"
26 26 # include "incls/_codeCache.cpp.incl"
27 27
28 28 // Helper class for printing in CodeCache
29 29
30 30 class CodeBlob_sizes {
31 31 private:
32 32 int count;
33 33 int total_size;
34 34 int header_size;
35 35 int code_size;
36 36 int stub_size;
37 37 int relocation_size;
38 38 int scopes_oop_size;
39 39 int scopes_data_size;
40 40 int scopes_pcs_size;
41 41
42 42 public:
43 43 CodeBlob_sizes() {
44 44 count = 0;
45 45 total_size = 0;
46 46 header_size = 0;
47 47 code_size = 0;
48 48 stub_size = 0;
49 49 relocation_size = 0;
50 50 scopes_oop_size = 0;
51 51 scopes_data_size = 0;
52 52 scopes_pcs_size = 0;
53 53 }
54 54
55 55 int total() { return total_size; }
56 56 bool is_empty() { return count == 0; }
57 57
58 58 void print(const char* title) {
59 59 tty->print_cr(" #%d %s = %dK (hdr %d%%, loc %d%%, code %d%%, stub %d%%, [oops %d%%, data %d%%, pcs %d%%])",
60 60 count,
61 61 title,
62 62 total() / K,
63 63 header_size * 100 / total_size,
64 64 relocation_size * 100 / total_size,
65 65 code_size * 100 / total_size,
66 66 stub_size * 100 / total_size,
↓ open down ↓ |
66 lines elided |
↑ open up ↑ |
67 67 scopes_oop_size * 100 / total_size,
68 68 scopes_data_size * 100 / total_size,
69 69 scopes_pcs_size * 100 / total_size);
70 70 }
71 71
72 72 void add(CodeBlob* cb) {
73 73 count++;
74 74 total_size += cb->size();
75 75 header_size += cb->header_size();
76 76 relocation_size += cb->relocation_size();
77 - scopes_oop_size += cb->oops_size();
78 77 if (cb->is_nmethod()) {
79 - nmethod *nm = (nmethod*)cb;
78 + nmethod* nm = cb->as_nmethod_or_null();
80 79 code_size += nm->code_size();
81 80 stub_size += nm->stub_size();
82 81
82 + scopes_oop_size += nm->oops_size();
83 83 scopes_data_size += nm->scopes_data_size();
84 84 scopes_pcs_size += nm->scopes_pcs_size();
85 85 } else {
86 86 code_size += cb->instructions_size();
87 87 }
88 88 }
89 89 };
90 90
91 91
92 92 // CodeCache implementation
93 93
94 94 CodeHeap * CodeCache::_heap = new CodeHeap();
95 95 int CodeCache::_number_of_blobs = 0;
96 96 int CodeCache::_number_of_nmethods_with_dependencies = 0;
97 97 bool CodeCache::_needs_cache_clean = false;
98 98 nmethod* CodeCache::_scavenge_root_nmethods = NULL;
99 99 nmethod* CodeCache::_saved_nmethods = NULL;
100 100
101 101
102 102 CodeBlob* CodeCache::first() {
103 103 assert_locked_or_safepoint(CodeCache_lock);
104 104 return (CodeBlob*)_heap->first();
105 105 }
106 106
107 107
108 108 CodeBlob* CodeCache::next(CodeBlob* cb) {
109 109 assert_locked_or_safepoint(CodeCache_lock);
110 110 return (CodeBlob*)_heap->next(cb);
111 111 }
112 112
113 113
114 114 CodeBlob* CodeCache::alive(CodeBlob *cb) {
115 115 assert_locked_or_safepoint(CodeCache_lock);
116 116 while (cb != NULL && !cb->is_alive()) cb = next(cb);
117 117 return cb;
118 118 }
119 119
120 120
121 121 nmethod* CodeCache::alive_nmethod(CodeBlob* cb) {
122 122 assert_locked_or_safepoint(CodeCache_lock);
123 123 while (cb != NULL && (!cb->is_alive() || !cb->is_nmethod())) cb = next(cb);
124 124 return (nmethod*)cb;
125 125 }
126 126
127 127 nmethod* CodeCache::first_nmethod() {
128 128 assert_locked_or_safepoint(CodeCache_lock);
129 129 CodeBlob* cb = first();
130 130 while (cb != NULL && !cb->is_nmethod()) {
131 131 cb = next(cb);
132 132 }
133 133 return (nmethod*)cb;
134 134 }
135 135
136 136 nmethod* CodeCache::next_nmethod (CodeBlob* cb) {
137 137 assert_locked_or_safepoint(CodeCache_lock);
138 138 cb = next(cb);
139 139 while (cb != NULL && !cb->is_nmethod()) {
140 140 cb = next(cb);
141 141 }
142 142 return (nmethod*)cb;
143 143 }
144 144
145 145 CodeBlob* CodeCache::allocate(int size) {
146 146 // Do not seize the CodeCache lock here--if the caller has not
147 147 // already done so, we are going to lose bigtime, since the code
148 148 // cache will contain a garbage CodeBlob until the caller can
149 149 // run the constructor for the CodeBlob subclass he is busy
150 150 // instantiating.
151 151 guarantee(size >= 0, "allocation request must be reasonable");
152 152 assert_locked_or_safepoint(CodeCache_lock);
153 153 CodeBlob* cb = NULL;
154 154 _number_of_blobs++;
155 155 while (true) {
156 156 cb = (CodeBlob*)_heap->allocate(size);
157 157 if (cb != NULL) break;
158 158 if (!_heap->expand_by(CodeCacheExpansionSize)) {
159 159 // Expansion failed
160 160 return NULL;
161 161 }
162 162 if (PrintCodeCacheExtension) {
163 163 ResourceMark rm;
164 164 tty->print_cr("code cache extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (%d bytes)",
165 165 (intptr_t)_heap->begin(), (intptr_t)_heap->end(),
166 166 (address)_heap->end() - (address)_heap->begin());
167 167 }
168 168 }
169 169 verify_if_often();
170 170 print_trace("allocation", cb, size);
171 171 return cb;
172 172 }
173 173
174 174 void CodeCache::free(CodeBlob* cb) {
175 175 assert_locked_or_safepoint(CodeCache_lock);
176 176 verify_if_often();
177 177
178 178 print_trace("free", cb);
179 179 if (cb->is_nmethod() && ((nmethod *)cb)->has_dependencies()) {
180 180 _number_of_nmethods_with_dependencies--;
181 181 }
182 182 _number_of_blobs--;
183 183
184 184 _heap->deallocate(cb);
185 185
186 186 verify_if_often();
187 187 assert(_number_of_blobs >= 0, "sanity check");
188 188 }
189 189
190 190
191 191 void CodeCache::commit(CodeBlob* cb) {
192 192 // this is called by nmethod::nmethod, which must already own CodeCache_lock
193 193 assert_locked_or_safepoint(CodeCache_lock);
194 194 if (cb->is_nmethod() && ((nmethod *)cb)->has_dependencies()) {
195 195 _number_of_nmethods_with_dependencies++;
196 196 }
197 197 // flush the hardware I-cache
198 198 ICache::invalidate_range(cb->instructions_begin(), cb->instructions_size());
199 199 }
200 200
201 201
202 202 void CodeCache::flush() {
203 203 assert_locked_or_safepoint(CodeCache_lock);
204 204 Unimplemented();
205 205 }
206 206
207 207
208 208 // Iteration over CodeBlobs
209 209
210 210 #define FOR_ALL_BLOBS(var) for (CodeBlob *var = first() ; var != NULL; var = next(var) )
211 211 #define FOR_ALL_ALIVE_BLOBS(var) for (CodeBlob *var = alive(first()); var != NULL; var = alive(next(var)))
212 212 #define FOR_ALL_ALIVE_NMETHODS(var) for (nmethod *var = alive_nmethod(first()); var != NULL; var = alive_nmethod(next(var)))
213 213
214 214
215 215 bool CodeCache::contains(void *p) {
216 216 // It should be ok to call contains without holding a lock
217 217 return _heap->contains(p);
218 218 }
219 219
220 220
221 221 // This method is safe to call without holding the CodeCache_lock, as long as a dead codeblob is not
222 222 // looked up (i.e., one that has been marked for deletion). It only dependes on the _segmap to contain
223 223 // valid indices, which it will always do, as long as the CodeBlob is not in the process of being recycled.
224 224 CodeBlob* CodeCache::find_blob(void* start) {
225 225 CodeBlob* result = find_blob_unsafe(start);
226 226 if (result == NULL) return NULL;
227 227 // We could potientially look up non_entrant methods
228 228 guarantee(!result->is_zombie() || result->is_locked_by_vm() || is_error_reported(), "unsafe access to zombie method");
229 229 return result;
230 230 }
231 231
232 232 nmethod* CodeCache::find_nmethod(void* start) {
233 233 CodeBlob *cb = find_blob(start);
234 234 assert(cb == NULL || cb->is_nmethod(), "did not find an nmethod");
235 235 return (nmethod*)cb;
236 236 }
237 237
238 238
239 239 void CodeCache::blobs_do(void f(CodeBlob* nm)) {
240 240 assert_locked_or_safepoint(CodeCache_lock);
241 241 FOR_ALL_BLOBS(p) {
242 242 f(p);
243 243 }
244 244 }
245 245
246 246
247 247 void CodeCache::nmethods_do(void f(nmethod* nm)) {
248 248 assert_locked_or_safepoint(CodeCache_lock);
249 249 FOR_ALL_BLOBS(nm) {
250 250 if (nm->is_nmethod()) f((nmethod*)nm);
251 251 }
252 252 }
253 253
254 254
↓ open down ↓ |
162 lines elided |
↑ open up ↑ |
255 255 int CodeCache::alignment_unit() {
256 256 return (int)_heap->alignment_unit();
257 257 }
258 258
259 259
260 260 int CodeCache::alignment_offset() {
261 261 return (int)_heap->alignment_offset();
262 262 }
263 263
264 264
265 -// Mark code blobs for unloading if they contain otherwise
266 -// unreachable oops.
265 +// Mark nmethods for unloading if they contain otherwise unreachable
266 +// oops.
267 267 void CodeCache::do_unloading(BoolObjectClosure* is_alive,
268 268 OopClosure* keep_alive,
269 269 bool unloading_occurred) {
270 270 assert_locked_or_safepoint(CodeCache_lock);
271 - FOR_ALL_ALIVE_BLOBS(cb) {
272 - cb->do_unloading(is_alive, keep_alive, unloading_occurred);
271 + FOR_ALL_ALIVE_NMETHODS(nm) {
272 + nm->do_unloading(is_alive, keep_alive, unloading_occurred);
273 273 }
274 274 }
275 275
276 276 void CodeCache::blobs_do(CodeBlobClosure* f) {
277 277 assert_locked_or_safepoint(CodeCache_lock);
278 278 FOR_ALL_ALIVE_BLOBS(cb) {
279 279 f->do_code_blob(cb);
280 280
281 281 #ifdef ASSERT
282 282 if (cb->is_nmethod())
283 283 ((nmethod*)cb)->verify_scavenge_root_oops();
284 284 #endif //ASSERT
285 285 }
286 286 }
287 287
288 288 // Walk the list of methods which might contain non-perm oops.
289 289 void CodeCache::scavenge_root_nmethods_do(CodeBlobClosure* f) {
290 290 assert_locked_or_safepoint(CodeCache_lock);
291 291 debug_only(mark_scavenge_root_nmethods());
292 292
293 293 for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) {
294 294 debug_only(cur->clear_scavenge_root_marked());
295 295 assert(cur->scavenge_root_not_marked(), "");
296 296 assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
297 297
298 298 bool is_live = (!cur->is_zombie() && !cur->is_unloaded());
299 299 #ifndef PRODUCT
300 300 if (TraceScavenge) {
301 301 cur->print_on(tty, is_live ? "scavenge root" : "dead scavenge root"); tty->cr();
302 302 }
303 303 #endif //PRODUCT
304 304 if (is_live) {
305 305 // Perform cur->oops_do(f), maybe just once per nmethod.
306 306 f->do_code_blob(cur);
307 307 cur->fix_oop_relocations();
308 308 }
309 309 }
310 310
311 311 // Check for stray marks.
312 312 debug_only(verify_perm_nmethods(NULL));
313 313 }
314 314
315 315 void CodeCache::add_scavenge_root_nmethod(nmethod* nm) {
316 316 assert_locked_or_safepoint(CodeCache_lock);
317 317 nm->set_on_scavenge_root_list();
318 318 nm->set_scavenge_root_link(_scavenge_root_nmethods);
319 319 set_scavenge_root_nmethods(nm);
320 320 print_trace("add_scavenge_root", nm);
321 321 }
322 322
323 323 void CodeCache::drop_scavenge_root_nmethod(nmethod* nm) {
324 324 assert_locked_or_safepoint(CodeCache_lock);
325 325 print_trace("drop_scavenge_root", nm);
326 326 nmethod* last = NULL;
327 327 nmethod* cur = scavenge_root_nmethods();
328 328 while (cur != NULL) {
329 329 nmethod* next = cur->scavenge_root_link();
330 330 if (cur == nm) {
331 331 if (last != NULL)
332 332 last->set_scavenge_root_link(next);
333 333 else set_scavenge_root_nmethods(next);
334 334 nm->set_scavenge_root_link(NULL);
335 335 nm->clear_on_scavenge_root_list();
336 336 return;
337 337 }
338 338 last = cur;
339 339 cur = next;
340 340 }
341 341 assert(false, "should have been on list");
342 342 }
343 343
344 344 void CodeCache::prune_scavenge_root_nmethods() {
345 345 assert_locked_or_safepoint(CodeCache_lock);
346 346 debug_only(mark_scavenge_root_nmethods());
347 347
348 348 nmethod* last = NULL;
349 349 nmethod* cur = scavenge_root_nmethods();
350 350 while (cur != NULL) {
351 351 nmethod* next = cur->scavenge_root_link();
352 352 debug_only(cur->clear_scavenge_root_marked());
353 353 assert(cur->scavenge_root_not_marked(), "");
354 354 assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
355 355
356 356 if (!cur->is_zombie() && !cur->is_unloaded()
357 357 && cur->detect_scavenge_root_oops()) {
358 358 // Keep it. Advance 'last' to prevent deletion.
359 359 last = cur;
360 360 } else {
361 361 // Prune it from the list, so we don't have to look at it any more.
362 362 print_trace("prune_scavenge_root", cur);
363 363 cur->set_scavenge_root_link(NULL);
364 364 cur->clear_on_scavenge_root_list();
365 365 if (last != NULL)
366 366 last->set_scavenge_root_link(next);
367 367 else set_scavenge_root_nmethods(next);
368 368 }
369 369 cur = next;
370 370 }
371 371
372 372 // Check for stray marks.
373 373 debug_only(verify_perm_nmethods(NULL));
374 374 }
375 375
376 376 #ifndef PRODUCT
377 377 void CodeCache::asserted_non_scavengable_nmethods_do(CodeBlobClosure* f) {
378 378 // While we are here, verify the integrity of the list.
379 379 mark_scavenge_root_nmethods();
380 380 for (nmethod* cur = scavenge_root_nmethods(); cur != NULL; cur = cur->scavenge_root_link()) {
381 381 assert(cur->on_scavenge_root_list(), "else shouldn't be on this list");
382 382 cur->clear_scavenge_root_marked();
383 383 }
384 384 verify_perm_nmethods(f);
385 385 }
386 386
387 387 // Temporarily mark nmethods that are claimed to be on the non-perm list.
388 388 void CodeCache::mark_scavenge_root_nmethods() {
389 389 FOR_ALL_ALIVE_BLOBS(cb) {
390 390 if (cb->is_nmethod()) {
391 391 nmethod *nm = (nmethod*)cb;
392 392 assert(nm->scavenge_root_not_marked(), "clean state");
393 393 if (nm->on_scavenge_root_list())
394 394 nm->set_scavenge_root_marked();
395 395 }
396 396 }
397 397 }
398 398
399 399 // If the closure is given, run it on the unlisted nmethods.
400 400 // Also make sure that the effects of mark_scavenge_root_nmethods is gone.
401 401 void CodeCache::verify_perm_nmethods(CodeBlobClosure* f_or_null) {
402 402 FOR_ALL_ALIVE_BLOBS(cb) {
403 403 bool call_f = (f_or_null != NULL);
404 404 if (cb->is_nmethod()) {
405 405 nmethod *nm = (nmethod*)cb;
406 406 assert(nm->scavenge_root_not_marked(), "must be already processed");
407 407 if (nm->on_scavenge_root_list())
408 408 call_f = false; // don't show this one to the client
409 409 nm->verify_scavenge_root_oops();
410 410 } else {
411 411 call_f = false; // not an nmethod
412 412 }
413 413 if (call_f) f_or_null->do_code_blob(cb);
414 414 }
415 415 }
416 416 #endif //PRODUCT
417 417
418 418
419 419 nmethod* CodeCache::find_and_remove_saved_code(methodOop m) {
420 420 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
421 421 nmethod* saved = _saved_nmethods;
422 422 nmethod* prev = NULL;
423 423 while (saved != NULL) {
424 424 if (saved->is_in_use() && saved->method() == m) {
425 425 if (prev != NULL) {
426 426 prev->set_saved_nmethod_link(saved->saved_nmethod_link());
427 427 } else {
428 428 _saved_nmethods = saved->saved_nmethod_link();
429 429 }
430 430 assert(saved->is_speculatively_disconnected(), "shouldn't call for other nmethods");
431 431 saved->set_speculatively_disconnected(false);
432 432 saved->set_saved_nmethod_link(NULL);
433 433 if (PrintMethodFlushing) {
434 434 saved->print_on(tty, " ### nmethod is reconnected\n");
435 435 }
436 436 if (LogCompilation && (xtty != NULL)) {
437 437 ttyLocker ttyl;
438 438 xtty->begin_elem("nmethod_reconnected compile_id='%3d'", saved->compile_id());
439 439 xtty->method(methodOop(m));
440 440 xtty->stamp();
441 441 xtty->end_elem();
442 442 }
443 443 return saved;
444 444 }
445 445 prev = saved;
446 446 saved = saved->saved_nmethod_link();
447 447 }
448 448 return NULL;
449 449 }
450 450
451 451 void CodeCache::remove_saved_code(nmethod* nm) {
452 452 // For conc swpr this will be called with CodeCache_lock taken by caller
453 453 assert_locked_or_safepoint(CodeCache_lock);
454 454 assert(nm->is_speculatively_disconnected(), "shouldn't call for other nmethods");
455 455 nmethod* saved = _saved_nmethods;
456 456 nmethod* prev = NULL;
457 457 while (saved != NULL) {
458 458 if (saved == nm) {
459 459 if (prev != NULL) {
460 460 prev->set_saved_nmethod_link(saved->saved_nmethod_link());
461 461 } else {
462 462 _saved_nmethods = saved->saved_nmethod_link();
463 463 }
464 464 if (LogCompilation && (xtty != NULL)) {
465 465 ttyLocker ttyl;
466 466 xtty->begin_elem("nmethod_removed compile_id='%3d'", nm->compile_id());
467 467 xtty->stamp();
468 468 xtty->end_elem();
469 469 }
470 470 return;
471 471 }
472 472 prev = saved;
473 473 saved = saved->saved_nmethod_link();
474 474 }
475 475 ShouldNotReachHere();
476 476 }
477 477
478 478 void CodeCache::speculatively_disconnect(nmethod* nm) {
479 479 assert_locked_or_safepoint(CodeCache_lock);
480 480 assert(nm->is_in_use() && !nm->is_speculatively_disconnected(), "should only disconnect live nmethods");
481 481 nm->set_saved_nmethod_link(_saved_nmethods);
482 482 _saved_nmethods = nm;
483 483 if (PrintMethodFlushing) {
484 484 nm->print_on(tty, " ### nmethod is speculatively disconnected\n");
485 485 }
486 486 if (LogCompilation && (xtty != NULL)) {
487 487 ttyLocker ttyl;
488 488 xtty->begin_elem("nmethod_disconnected compile_id='%3d'", nm->compile_id());
489 489 xtty->method(methodOop(nm->method()));
490 490 xtty->stamp();
491 491 xtty->end_elem();
492 492 }
493 493 nm->method()->clear_code();
494 494 nm->set_speculatively_disconnected(true);
495 495 }
496 496
497 497
498 498 void CodeCache::gc_prologue() {
499 499 assert(!nmethod::oops_do_marking_is_active(), "oops_do_marking_epilogue must be called");
500 500 }
501 501
↓ open down ↓ |
219 lines elided |
↑ open up ↑ |
502 502
503 503 void CodeCache::gc_epilogue() {
504 504 assert_locked_or_safepoint(CodeCache_lock);
505 505 FOR_ALL_ALIVE_BLOBS(cb) {
506 506 if (cb->is_nmethod()) {
507 507 nmethod *nm = (nmethod*)cb;
508 508 assert(!nm->is_unloaded(), "Tautology");
509 509 if (needs_cache_clean()) {
510 510 nm->cleanup_inline_caches();
511 511 }
512 - debug_only(nm->verify();)
512 + DEBUG_ONLY(nm->verify());
513 + nm->fix_oop_relocations();
513 514 }
514 - cb->fix_oop_relocations();
515 515 }
516 516 set_needs_cache_clean(false);
517 517 prune_scavenge_root_nmethods();
518 518 assert(!nmethod::oops_do_marking_is_active(), "oops_do_marking_prologue must be called");
519 519 }
520 520
521 521
522 522 address CodeCache::first_address() {
523 523 assert_locked_or_safepoint(CodeCache_lock);
524 524 return (address)_heap->begin();
525 525 }
526 526
527 527
528 528 address CodeCache::last_address() {
529 529 assert_locked_or_safepoint(CodeCache_lock);
530 530 return (address)_heap->end();
531 531 }
532 532
533 533
534 534 void icache_init();
535 535
536 536 void CodeCache::initialize() {
537 537 assert(CodeCacheSegmentSize >= (uintx)CodeEntryAlignment, "CodeCacheSegmentSize must be large enough to align entry points");
538 538 #ifdef COMPILER2
539 539 assert(CodeCacheSegmentSize >= (uintx)OptoLoopAlignment, "CodeCacheSegmentSize must be large enough to align inner loops");
540 540 #endif
541 541 assert(CodeCacheSegmentSize >= sizeof(jdouble), "CodeCacheSegmentSize must be large enough to align constants");
542 542 // This was originally just a check of the alignment, causing failure, instead, round
543 543 // the code cache to the page size. In particular, Solaris is moving to a larger
544 544 // default page size.
545 545 CodeCacheExpansionSize = round_to(CodeCacheExpansionSize, os::vm_page_size());
546 546 InitialCodeCacheSize = round_to(InitialCodeCacheSize, os::vm_page_size());
547 547 ReservedCodeCacheSize = round_to(ReservedCodeCacheSize, os::vm_page_size());
548 548 if (!_heap->reserve(ReservedCodeCacheSize, InitialCodeCacheSize, CodeCacheSegmentSize)) {
549 549 vm_exit_during_initialization("Could not reserve enough space for code cache");
550 550 }
551 551
552 552 MemoryService::add_code_heap_memory_pool(_heap);
553 553
554 554 // Initialize ICache flush mechanism
555 555 // This service is needed for os::register_code_area
556 556 icache_init();
557 557
558 558 // Give OS a chance to register generated code area.
559 559 // This is used on Windows 64 bit platforms to register
560 560 // Structured Exception Handlers for our generated code.
561 561 os::register_code_area(_heap->low_boundary(), _heap->high_boundary());
562 562 }
563 563
564 564
565 565 void codeCache_init() {
566 566 CodeCache::initialize();
567 567 }
568 568
569 569 //------------------------------------------------------------------------------------------------
570 570
571 571 int CodeCache::number_of_nmethods_with_dependencies() {
572 572 return _number_of_nmethods_with_dependencies;
573 573 }
574 574
575 575 void CodeCache::clear_inline_caches() {
576 576 assert_locked_or_safepoint(CodeCache_lock);
577 577 FOR_ALL_ALIVE_NMETHODS(nm) {
578 578 nm->clear_inline_caches();
579 579 }
580 580 }
581 581
582 582 #ifndef PRODUCT
583 583 // used to keep track of how much time is spent in mark_for_deoptimization
584 584 static elapsedTimer dependentCheckTime;
585 585 static int dependentCheckCount = 0;
586 586 #endif // PRODUCT
587 587
588 588
589 589 int CodeCache::mark_for_deoptimization(DepChange& changes) {
590 590 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
591 591
592 592 #ifndef PRODUCT
593 593 dependentCheckTime.start();
594 594 dependentCheckCount++;
595 595 #endif // PRODUCT
596 596
597 597 int number_of_marked_CodeBlobs = 0;
598 598
599 599 // search the hierarchy looking for nmethods which are affected by the loading of this class
600 600
601 601 // then search the interfaces this class implements looking for nmethods
602 602 // which might be dependent of the fact that an interface only had one
603 603 // implementor.
604 604
605 605 { No_Safepoint_Verifier nsv;
606 606 for (DepChange::ContextStream str(changes, nsv); str.next(); ) {
607 607 klassOop d = str.klass();
608 608 number_of_marked_CodeBlobs += instanceKlass::cast(d)->mark_dependent_nmethods(changes);
609 609 }
610 610 }
611 611
612 612 if (VerifyDependencies) {
613 613 // Turn off dependency tracing while actually testing deps.
614 614 NOT_PRODUCT( FlagSetting fs(TraceDependencies, false) );
615 615 FOR_ALL_ALIVE_NMETHODS(nm) {
616 616 if (!nm->is_marked_for_deoptimization() &&
617 617 nm->check_all_dependencies()) {
618 618 ResourceMark rm;
619 619 tty->print_cr("Should have been marked for deoptimization:");
620 620 changes.print();
621 621 nm->print();
622 622 nm->print_dependencies();
623 623 }
624 624 }
625 625 }
626 626
627 627 #ifndef PRODUCT
628 628 dependentCheckTime.stop();
629 629 #endif // PRODUCT
630 630
631 631 return number_of_marked_CodeBlobs;
632 632 }
633 633
634 634
635 635 #ifdef HOTSWAP
636 636 int CodeCache::mark_for_evol_deoptimization(instanceKlassHandle dependee) {
637 637 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
638 638 int number_of_marked_CodeBlobs = 0;
639 639
640 640 // Deoptimize all methods of the evolving class itself
641 641 objArrayOop old_methods = dependee->methods();
642 642 for (int i = 0; i < old_methods->length(); i++) {
643 643 ResourceMark rm;
644 644 methodOop old_method = (methodOop) old_methods->obj_at(i);
645 645 nmethod *nm = old_method->code();
646 646 if (nm != NULL) {
647 647 nm->mark_for_deoptimization();
648 648 number_of_marked_CodeBlobs++;
649 649 }
650 650 }
651 651
652 652 FOR_ALL_ALIVE_NMETHODS(nm) {
653 653 if (nm->is_marked_for_deoptimization()) {
654 654 // ...Already marked in the previous pass; don't count it again.
655 655 } else if (nm->is_evol_dependent_on(dependee())) {
656 656 ResourceMark rm;
657 657 nm->mark_for_deoptimization();
658 658 number_of_marked_CodeBlobs++;
659 659 } else {
660 660 // flush caches in case they refer to a redefined methodOop
661 661 nm->clear_inline_caches();
662 662 }
663 663 }
664 664
665 665 return number_of_marked_CodeBlobs;
666 666 }
667 667 #endif // HOTSWAP
668 668
669 669
670 670 // Deoptimize all methods
671 671 void CodeCache::mark_all_nmethods_for_deoptimization() {
672 672 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
673 673 FOR_ALL_ALIVE_NMETHODS(nm) {
674 674 nm->mark_for_deoptimization();
675 675 }
676 676 }
677 677
678 678
679 679 int CodeCache::mark_for_deoptimization(methodOop dependee) {
680 680 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
681 681 int number_of_marked_CodeBlobs = 0;
682 682
683 683 FOR_ALL_ALIVE_NMETHODS(nm) {
684 684 if (nm->is_dependent_on_method(dependee)) {
685 685 ResourceMark rm;
686 686 nm->mark_for_deoptimization();
687 687 number_of_marked_CodeBlobs++;
688 688 }
689 689 }
690 690
691 691 return number_of_marked_CodeBlobs;
692 692 }
693 693
694 694 void CodeCache::make_marked_nmethods_zombies() {
695 695 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
696 696 FOR_ALL_ALIVE_NMETHODS(nm) {
697 697 if (nm->is_marked_for_deoptimization()) {
698 698
699 699 // If the nmethod has already been made non-entrant and it can be converted
700 700 // then zombie it now. Otherwise make it non-entrant and it will eventually
701 701 // be zombied when it is no longer seen on the stack. Note that the nmethod
702 702 // might be "entrant" and not on the stack and so could be zombied immediately
703 703 // but we can't tell because we don't track it on stack until it becomes
704 704 // non-entrant.
705 705
706 706 if (nm->is_not_entrant() && nm->can_not_entrant_be_converted()) {
707 707 nm->make_zombie();
708 708 } else {
709 709 nm->make_not_entrant();
710 710 }
711 711 }
712 712 }
713 713 }
714 714
715 715 void CodeCache::make_marked_nmethods_not_entrant() {
716 716 assert_locked_or_safepoint(CodeCache_lock);
717 717 FOR_ALL_ALIVE_NMETHODS(nm) {
718 718 if (nm->is_marked_for_deoptimization()) {
719 719 nm->make_not_entrant();
720 720 }
721 721 }
722 722 }
723 723
724 724 void CodeCache::verify() {
725 725 _heap->verify();
726 726 FOR_ALL_ALIVE_BLOBS(p) {
727 727 p->verify();
728 728 }
729 729 }
730 730
731 731 //------------------------------------------------------------------------------------------------
732 732 // Non-product version
733 733
734 734 #ifndef PRODUCT
735 735
736 736 void CodeCache::verify_if_often() {
737 737 if (VerifyCodeCacheOften) {
738 738 _heap->verify();
739 739 }
740 740 }
741 741
742 742 void CodeCache::print_trace(const char* event, CodeBlob* cb, int size) {
743 743 if (PrintCodeCache2) { // Need to add a new flag
744 744 ResourceMark rm;
745 745 if (size == 0) size = cb->size();
746 746 tty->print_cr("CodeCache %s: addr: " INTPTR_FORMAT ", size: 0x%x", event, cb, size);
747 747 }
748 748 }
749 749
750 750 void CodeCache::print_internals() {
751 751 int nmethodCount = 0;
752 752 int runtimeStubCount = 0;
753 753 int adapterCount = 0;
754 754 int deoptimizationStubCount = 0;
755 755 int uncommonTrapStubCount = 0;
756 756 int bufferBlobCount = 0;
757 757 int total = 0;
758 758 int nmethodAlive = 0;
759 759 int nmethodNotEntrant = 0;
760 760 int nmethodZombie = 0;
761 761 int nmethodUnloaded = 0;
762 762 int nmethodJava = 0;
763 763 int nmethodNative = 0;
764 764 int maxCodeSize = 0;
765 765 ResourceMark rm;
766 766
767 767 CodeBlob *cb;
768 768 for (cb = first(); cb != NULL; cb = next(cb)) {
769 769 total++;
770 770 if (cb->is_nmethod()) {
771 771 nmethod* nm = (nmethod*)cb;
772 772
773 773 if (Verbose && nm->method() != NULL) {
774 774 ResourceMark rm;
775 775 char *method_name = nm->method()->name_and_sig_as_C_string();
776 776 tty->print("%s", method_name);
777 777 if(nm->is_alive()) { tty->print_cr(" alive"); }
778 778 if(nm->is_not_entrant()) { tty->print_cr(" not-entrant"); }
779 779 if(nm->is_zombie()) { tty->print_cr(" zombie"); }
780 780 }
781 781
782 782 nmethodCount++;
783 783
784 784 if(nm->is_alive()) { nmethodAlive++; }
785 785 if(nm->is_not_entrant()) { nmethodNotEntrant++; }
786 786 if(nm->is_zombie()) { nmethodZombie++; }
787 787 if(nm->is_unloaded()) { nmethodUnloaded++; }
788 788 if(nm->is_native_method()) { nmethodNative++; }
789 789
790 790 if(nm->method() != NULL && nm->is_java_method()) {
791 791 nmethodJava++;
792 792 if(nm->code_size() > maxCodeSize) {
793 793 maxCodeSize = nm->code_size();
794 794 }
795 795 }
796 796 } else if (cb->is_runtime_stub()) {
797 797 runtimeStubCount++;
798 798 } else if (cb->is_deoptimization_stub()) {
799 799 deoptimizationStubCount++;
800 800 } else if (cb->is_uncommon_trap_stub()) {
801 801 uncommonTrapStubCount++;
802 802 } else if (cb->is_adapter_blob()) {
803 803 adapterCount++;
804 804 } else if (cb->is_buffer_blob()) {
805 805 bufferBlobCount++;
806 806 }
807 807 }
808 808
809 809 int bucketSize = 512;
810 810 int bucketLimit = maxCodeSize / bucketSize + 1;
811 811 int *buckets = NEW_C_HEAP_ARRAY(int, bucketLimit);
812 812 memset(buckets,0,sizeof(int) * bucketLimit);
813 813
814 814 for (cb = first(); cb != NULL; cb = next(cb)) {
815 815 if (cb->is_nmethod()) {
816 816 nmethod* nm = (nmethod*)cb;
817 817 if(nm->is_java_method()) {
818 818 buckets[nm->code_size() / bucketSize]++;
819 819 }
820 820 }
821 821 }
822 822 tty->print_cr("Code Cache Entries (total of %d)",total);
823 823 tty->print_cr("-------------------------------------------------");
824 824 tty->print_cr("nmethods: %d",nmethodCount);
825 825 tty->print_cr("\talive: %d",nmethodAlive);
826 826 tty->print_cr("\tnot_entrant: %d",nmethodNotEntrant);
827 827 tty->print_cr("\tzombie: %d",nmethodZombie);
828 828 tty->print_cr("\tunloaded: %d",nmethodUnloaded);
829 829 tty->print_cr("\tjava: %d",nmethodJava);
830 830 tty->print_cr("\tnative: %d",nmethodNative);
831 831 tty->print_cr("runtime_stubs: %d",runtimeStubCount);
832 832 tty->print_cr("adapters: %d",adapterCount);
833 833 tty->print_cr("buffer blobs: %d",bufferBlobCount);
834 834 tty->print_cr("deoptimization_stubs: %d",deoptimizationStubCount);
835 835 tty->print_cr("uncommon_traps: %d",uncommonTrapStubCount);
836 836 tty->print_cr("\nnmethod size distribution (non-zombie java)");
837 837 tty->print_cr("-------------------------------------------------");
838 838
839 839 for(int i=0; i<bucketLimit; i++) {
840 840 if(buckets[i] != 0) {
841 841 tty->print("%d - %d bytes",i*bucketSize,(i+1)*bucketSize);
842 842 tty->fill_to(40);
843 843 tty->print_cr("%d",buckets[i]);
844 844 }
845 845 }
846 846
847 847 FREE_C_HEAP_ARRAY(int, buckets);
848 848 }
849 849
850 850 void CodeCache::print() {
851 851 CodeBlob_sizes live;
852 852 CodeBlob_sizes dead;
853 853
854 854 FOR_ALL_BLOBS(p) {
855 855 if (!p->is_alive()) {
856 856 dead.add(p);
857 857 } else {
858 858 live.add(p);
859 859 }
860 860 }
861 861
862 862 tty->print_cr("CodeCache:");
863 863
864 864 tty->print_cr("nmethod dependency checking time %f", dependentCheckTime.seconds(),
865 865 dependentCheckTime.seconds() / dependentCheckCount);
866 866
867 867 if (!live.is_empty()) {
868 868 live.print("live");
869 869 }
870 870 if (!dead.is_empty()) {
871 871 dead.print("dead");
872 872 }
873 873
874 874
875 875 if (Verbose) {
876 876 // print the oop_map usage
877 877 int code_size = 0;
878 878 int number_of_blobs = 0;
879 879 int number_of_oop_maps = 0;
880 880 int map_size = 0;
881 881 FOR_ALL_BLOBS(p) {
882 882 if (p->is_alive()) {
883 883 number_of_blobs++;
884 884 code_size += p->instructions_size();
885 885 OopMapSet* set = p->oop_maps();
886 886 if (set != NULL) {
887 887 number_of_oop_maps += set->size();
888 888 map_size += set->heap_size();
889 889 }
890 890 }
891 891 }
892 892 tty->print_cr("OopMaps");
893 893 tty->print_cr(" #blobs = %d", number_of_blobs);
894 894 tty->print_cr(" code size = %d", code_size);
895 895 tty->print_cr(" #oop_maps = %d", number_of_oop_maps);
896 896 tty->print_cr(" map size = %d", map_size);
897 897 }
898 898
899 899 }
900 900
901 901 #endif // PRODUCT
↓ open down ↓ |
377 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX