comparison src/share/vm/code/codeCache.cpp @ 0:a61af66fc99e jdk7-b24

Initial load
author duke
date Sat, 01 Dec 2007 00:00:00 +0000
parents
children 148e5441d916
comparison
equal deleted inserted replaced
-1:000000000000 0:a61af66fc99e
1 /*
2 * Copyright 1997-2007 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 *
23 */
24
25 # include "incls/_precompiled.incl"
26 # include "incls/_codeCache.cpp.incl"
27
28 // Helper class for printing in CodeCache
29
30 class CodeBlob_sizes {
31 private:
32 int count;
33 int total_size;
34 int header_size;
35 int code_size;
36 int stub_size;
37 int relocation_size;
38 int scopes_oop_size;
39 int scopes_data_size;
40 int scopes_pcs_size;
41
42 public:
43 CodeBlob_sizes() {
44 count = 0;
45 total_size = 0;
46 header_size = 0;
47 code_size = 0;
48 stub_size = 0;
49 relocation_size = 0;
50 scopes_oop_size = 0;
51 scopes_data_size = 0;
52 scopes_pcs_size = 0;
53 }
54
55 int total() { return total_size; }
56 bool is_empty() { return count == 0; }
57
58 void print(const char* title) {
59 tty->print_cr(" #%d %s = %dK (hdr %d%%, loc %d%%, code %d%%, stub %d%%, [oops %d%%, data %d%%, pcs %d%%])",
60 count,
61 title,
62 total() / K,
63 header_size * 100 / total_size,
64 relocation_size * 100 / total_size,
65 code_size * 100 / total_size,
66 stub_size * 100 / total_size,
67 scopes_oop_size * 100 / total_size,
68 scopes_data_size * 100 / total_size,
69 scopes_pcs_size * 100 / total_size);
70 }
71
72 void add(CodeBlob* cb) {
73 count++;
74 total_size += cb->size();
75 header_size += cb->header_size();
76 relocation_size += cb->relocation_size();
77 scopes_oop_size += cb->oops_size();
78 if (cb->is_nmethod()) {
79 nmethod *nm = (nmethod*)cb;
80 code_size += nm->code_size();
81 stub_size += nm->stub_size();
82
83 scopes_data_size += nm->scopes_data_size();
84 scopes_pcs_size += nm->scopes_pcs_size();
85 } else {
86 code_size += cb->instructions_size();
87 }
88 }
89 };
90
91
92 // CodeCache implementation
93
94 CodeHeap * CodeCache::_heap = new CodeHeap();
95 int CodeCache::_number_of_blobs = 0;
96 int CodeCache::_number_of_nmethods_with_dependencies = 0;
97 bool CodeCache::_needs_cache_clean = false;
98
99
100 CodeBlob* CodeCache::first() {
101 assert_locked_or_safepoint(CodeCache_lock);
102 return (CodeBlob*)_heap->first();
103 }
104
105
106 CodeBlob* CodeCache::next(CodeBlob* cb) {
107 assert_locked_or_safepoint(CodeCache_lock);
108 return (CodeBlob*)_heap->next(cb);
109 }
110
111
112 CodeBlob* CodeCache::alive(CodeBlob *cb) {
113 assert_locked_or_safepoint(CodeCache_lock);
114 while (cb != NULL && !cb->is_alive()) cb = next(cb);
115 return cb;
116 }
117
118
119 nmethod* CodeCache::alive_nmethod(CodeBlob* cb) {
120 assert_locked_or_safepoint(CodeCache_lock);
121 while (cb != NULL && (!cb->is_alive() || !cb->is_nmethod())) cb = next(cb);
122 return (nmethod*)cb;
123 }
124
125
126 CodeBlob* CodeCache::allocate(int size) {
127 // Do not seize the CodeCache lock here--if the caller has not
128 // already done so, we are going to lose bigtime, since the code
129 // cache will contain a garbage CodeBlob until the caller can
130 // run the constructor for the CodeBlob subclass he is busy
131 // instantiating.
132 guarantee(size >= 0, "allocation request must be reasonable");
133 assert_locked_or_safepoint(CodeCache_lock);
134 CodeBlob* cb = NULL;
135 _number_of_blobs++;
136 while (true) {
137 cb = (CodeBlob*)_heap->allocate(size);
138 if (cb != NULL) break;
139 if (!_heap->expand_by(CodeCacheExpansionSize)) {
140 // Expansion failed
141 return NULL;
142 }
143 if (PrintCodeCacheExtension) {
144 ResourceMark rm;
145 tty->print_cr("code cache extended to [" INTPTR_FORMAT ", " INTPTR_FORMAT "] (%d bytes)",
146 (intptr_t)_heap->begin(), (intptr_t)_heap->end(),
147 (address)_heap->end() - (address)_heap->begin());
148 }
149 }
150 verify_if_often();
151 if (PrintCodeCache2) { // Need to add a new flag
152 ResourceMark rm;
153 tty->print_cr("CodeCache allocation: addr: " INTPTR_FORMAT ", size: 0x%x\n", cb, size);
154 }
155 return cb;
156 }
157
158 void CodeCache::free(CodeBlob* cb) {
159 assert_locked_or_safepoint(CodeCache_lock);
160 verify_if_often();
161
162 if (PrintCodeCache2) { // Need to add a new flag
163 ResourceMark rm;
164 tty->print_cr("CodeCache free: addr: " INTPTR_FORMAT ", size: 0x%x\n", cb, cb->size());
165 }
166 if (cb->is_nmethod() && ((nmethod *)cb)->has_dependencies()) {
167 _number_of_nmethods_with_dependencies--;
168 }
169 _number_of_blobs--;
170
171 _heap->deallocate(cb);
172
173 verify_if_often();
174 assert(_number_of_blobs >= 0, "sanity check");
175 }
176
177
178 void CodeCache::commit(CodeBlob* cb) {
179 // this is called by nmethod::nmethod, which must already own CodeCache_lock
180 assert_locked_or_safepoint(CodeCache_lock);
181 if (cb->is_nmethod() && ((nmethod *)cb)->has_dependencies()) {
182 _number_of_nmethods_with_dependencies++;
183 }
184 // flush the hardware I-cache
185 ICache::invalidate_range(cb->instructions_begin(), cb->instructions_size());
186 }
187
188
189 void CodeCache::flush() {
190 assert_locked_or_safepoint(CodeCache_lock);
191 Unimplemented();
192 }
193
194
195 // Iteration over CodeBlobs
196
197 #define FOR_ALL_BLOBS(var) for (CodeBlob *var = first() ; var != NULL; var = next(var) )
198 #define FOR_ALL_ALIVE_BLOBS(var) for (CodeBlob *var = alive(first()); var != NULL; var = alive(next(var)))
199 #define FOR_ALL_ALIVE_NMETHODS(var) for (nmethod *var = alive_nmethod(first()); var != NULL; var = alive_nmethod(next(var)))
200
201
202 bool CodeCache::contains(void *p) {
203 // It should be ok to call contains without holding a lock
204 return _heap->contains(p);
205 }
206
207
208 // This method is safe to call without holding the CodeCache_lock, as long as a dead codeblob is not
209 // looked up (i.e., one that has been marked for deletion). It only dependes on the _segmap to contain
210 // valid indices, which it will always do, as long as the CodeBlob is not in the process of being recycled.
211 CodeBlob* CodeCache::find_blob(void* start) {
212 CodeBlob* result = find_blob_unsafe(start);
213 if (result == NULL) return NULL;
214 // We could potientially look up non_entrant methods
215 guarantee(!result->is_zombie() || result->is_locked_by_vm() || is_error_reported(), "unsafe access to zombie method");
216 return result;
217 }
218
219 nmethod* CodeCache::find_nmethod(void* start) {
220 CodeBlob *cb = find_blob(start);
221 assert(cb == NULL || cb->is_nmethod(), "did not find an nmethod");
222 return (nmethod*)cb;
223 }
224
225
226 void CodeCache::blobs_do(void f(CodeBlob* nm)) {
227 assert_locked_or_safepoint(CodeCache_lock);
228 FOR_ALL_BLOBS(p) {
229 f(p);
230 }
231 }
232
233
234 void CodeCache::nmethods_do(void f(nmethod* nm)) {
235 assert_locked_or_safepoint(CodeCache_lock);
236 FOR_ALL_BLOBS(nm) {
237 if (nm->is_nmethod()) f((nmethod*)nm);
238 }
239 }
240
241
242 int CodeCache::alignment_unit() {
243 return (int)_heap->alignment_unit();
244 }
245
246
247 int CodeCache::alignment_offset() {
248 return (int)_heap->alignment_offset();
249 }
250
251
252 // Mark code blobs for unloading if they contain otherwise
253 // unreachable oops.
254 void CodeCache::do_unloading(BoolObjectClosure* is_alive,
255 OopClosure* keep_alive,
256 bool unloading_occurred) {
257 assert_locked_or_safepoint(CodeCache_lock);
258 FOR_ALL_ALIVE_BLOBS(cb) {
259 cb->do_unloading(is_alive, keep_alive, unloading_occurred);
260 }
261 }
262
263 void CodeCache::oops_do(OopClosure* f) {
264 assert_locked_or_safepoint(CodeCache_lock);
265 FOR_ALL_ALIVE_BLOBS(cb) {
266 cb->oops_do(f);
267 }
268 }
269
270 void CodeCache::gc_prologue() {
271 }
272
273
274 void CodeCache::gc_epilogue() {
275 assert_locked_or_safepoint(CodeCache_lock);
276 FOR_ALL_ALIVE_BLOBS(cb) {
277 if (cb->is_nmethod()) {
278 nmethod *nm = (nmethod*)cb;
279 assert(!nm->is_unloaded(), "Tautology");
280 if (needs_cache_clean()) {
281 nm->cleanup_inline_caches();
282 }
283 debug_only(nm->verify();)
284 }
285 cb->fix_oop_relocations();
286 }
287 set_needs_cache_clean(false);
288 }
289
290
291 address CodeCache::first_address() {
292 assert_locked_or_safepoint(CodeCache_lock);
293 return (address)_heap->begin();
294 }
295
296
297 address CodeCache::last_address() {
298 assert_locked_or_safepoint(CodeCache_lock);
299 return (address)_heap->end();
300 }
301
302
303 void icache_init();
304
305 void CodeCache::initialize() {
306 assert(CodeCacheSegmentSize >= (uintx)CodeEntryAlignment, "CodeCacheSegmentSize must be large enough to align entry points");
307 #ifdef COMPILER2
308 assert(CodeCacheSegmentSize >= (uintx)OptoLoopAlignment, "CodeCacheSegmentSize must be large enough to align inner loops");
309 #endif
310 assert(CodeCacheSegmentSize >= sizeof(jdouble), "CodeCacheSegmentSize must be large enough to align constants");
311 // This was originally just a check of the alignment, causing failure, instead, round
312 // the code cache to the page size. In particular, Solaris is moving to a larger
313 // default page size.
314 CodeCacheExpansionSize = round_to(CodeCacheExpansionSize, os::vm_page_size());
315 InitialCodeCacheSize = round_to(InitialCodeCacheSize, os::vm_page_size());
316 ReservedCodeCacheSize = round_to(ReservedCodeCacheSize, os::vm_page_size());
317 if (!_heap->reserve(ReservedCodeCacheSize, InitialCodeCacheSize, CodeCacheSegmentSize)) {
318 vm_exit_during_initialization("Could not reserve enough space for code cache");
319 }
320
321 MemoryService::add_code_heap_memory_pool(_heap);
322
323 // Initialize ICache flush mechanism
324 // This service is needed for os::register_code_area
325 icache_init();
326
327 // Give OS a chance to register generated code area.
328 // This is used on Windows 64 bit platforms to register
329 // Structured Exception Handlers for our generated code.
330 os::register_code_area(_heap->low_boundary(), _heap->high_boundary());
331 }
332
333
334 void codeCache_init() {
335 CodeCache::initialize();
336 }
337
338 //------------------------------------------------------------------------------------------------
339
340 int CodeCache::number_of_nmethods_with_dependencies() {
341 return _number_of_nmethods_with_dependencies;
342 }
343
344 void CodeCache::clear_inline_caches() {
345 assert_locked_or_safepoint(CodeCache_lock);
346 FOR_ALL_ALIVE_NMETHODS(nm) {
347 nm->clear_inline_caches();
348 }
349 }
350
351 #ifndef PRODUCT
352 // used to keep track of how much time is spent in mark_for_deoptimization
353 static elapsedTimer dependentCheckTime;
354 static int dependentCheckCount = 0;
355 #endif // PRODUCT
356
357
358 int CodeCache::mark_for_deoptimization(DepChange& changes) {
359 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
360
361 #ifndef PRODUCT
362 dependentCheckTime.start();
363 dependentCheckCount++;
364 #endif // PRODUCT
365
366 int number_of_marked_CodeBlobs = 0;
367
368 // search the hierarchy looking for nmethods which are affected by the loading of this class
369
370 // then search the interfaces this class implements looking for nmethods
371 // which might be dependent of the fact that an interface only had one
372 // implementor.
373
374 { No_Safepoint_Verifier nsv;
375 for (DepChange::ContextStream str(changes, nsv); str.next(); ) {
376 klassOop d = str.klass();
377 number_of_marked_CodeBlobs += instanceKlass::cast(d)->mark_dependent_nmethods(changes);
378 }
379 }
380
381 if (VerifyDependencies) {
382 // Turn off dependency tracing while actually testing deps.
383 NOT_PRODUCT( FlagSetting fs(TraceDependencies, false) );
384 FOR_ALL_ALIVE_NMETHODS(nm) {
385 if (!nm->is_marked_for_deoptimization() &&
386 nm->check_all_dependencies()) {
387 ResourceMark rm;
388 tty->print_cr("Should have been marked for deoptimization:");
389 changes.print();
390 nm->print();
391 nm->print_dependencies();
392 }
393 }
394 }
395
396 #ifndef PRODUCT
397 dependentCheckTime.stop();
398 #endif // PRODUCT
399
400 return number_of_marked_CodeBlobs;
401 }
402
403
404 #ifdef HOTSWAP
405 int CodeCache::mark_for_evol_deoptimization(instanceKlassHandle dependee) {
406 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
407 int number_of_marked_CodeBlobs = 0;
408
409 // Deoptimize all methods of the evolving class itself
410 objArrayOop old_methods = dependee->methods();
411 for (int i = 0; i < old_methods->length(); i++) {
412 ResourceMark rm;
413 methodOop old_method = (methodOop) old_methods->obj_at(i);
414 nmethod *nm = old_method->code();
415 if (nm != NULL) {
416 nm->mark_for_deoptimization();
417 number_of_marked_CodeBlobs++;
418 }
419 }
420
421 FOR_ALL_ALIVE_NMETHODS(nm) {
422 if (nm->is_marked_for_deoptimization()) {
423 // ...Already marked in the previous pass; don't count it again.
424 } else if (nm->is_evol_dependent_on(dependee())) {
425 ResourceMark rm;
426 nm->mark_for_deoptimization();
427 number_of_marked_CodeBlobs++;
428 } else {
429 // flush caches in case they refer to a redefined methodOop
430 nm->clear_inline_caches();
431 }
432 }
433
434 return number_of_marked_CodeBlobs;
435 }
436 #endif // HOTSWAP
437
438
439 // Deoptimize all methods
440 void CodeCache::mark_all_nmethods_for_deoptimization() {
441 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
442 FOR_ALL_ALIVE_NMETHODS(nm) {
443 nm->mark_for_deoptimization();
444 }
445 }
446
447
448 int CodeCache::mark_for_deoptimization(methodOop dependee) {
449 MutexLockerEx mu(CodeCache_lock, Mutex::_no_safepoint_check_flag);
450 int number_of_marked_CodeBlobs = 0;
451
452 FOR_ALL_ALIVE_NMETHODS(nm) {
453 if (nm->is_dependent_on_method(dependee)) {
454 ResourceMark rm;
455 nm->mark_for_deoptimization();
456 number_of_marked_CodeBlobs++;
457 }
458 }
459
460 return number_of_marked_CodeBlobs;
461 }
462
463 void CodeCache::make_marked_nmethods_zombies() {
464 assert(SafepointSynchronize::is_at_safepoint(), "must be at a safepoint");
465 FOR_ALL_ALIVE_NMETHODS(nm) {
466 if (nm->is_marked_for_deoptimization()) {
467
468 // If the nmethod has already been made non-entrant and it can be converted
469 // then zombie it now. Otherwise make it non-entrant and it will eventually
470 // be zombied when it is no longer seen on the stack. Note that the nmethod
471 // might be "entrant" and not on the stack and so could be zombied immediately
472 // but we can't tell because we don't track it on stack until it becomes
473 // non-entrant.
474
475 if (nm->is_not_entrant() && nm->can_not_entrant_be_converted()) {
476 nm->make_zombie();
477 } else {
478 nm->make_not_entrant();
479 }
480 }
481 }
482 }
483
484 void CodeCache::make_marked_nmethods_not_entrant() {
485 assert_locked_or_safepoint(CodeCache_lock);
486 FOR_ALL_ALIVE_NMETHODS(nm) {
487 if (nm->is_marked_for_deoptimization()) {
488 nm->make_not_entrant();
489 }
490 }
491 }
492
493 void CodeCache::verify() {
494 _heap->verify();
495 FOR_ALL_ALIVE_BLOBS(p) {
496 p->verify();
497 }
498 }
499
500 //------------------------------------------------------------------------------------------------
501 // Non-product version
502
503 #ifndef PRODUCT
504
505 void CodeCache::verify_if_often() {
506 if (VerifyCodeCacheOften) {
507 _heap->verify();
508 }
509 }
510
511 void CodeCache::print_internals() {
512 int nmethodCount = 0;
513 int runtimeStubCount = 0;
514 int adapterCount = 0;
515 int deoptimizationStubCount = 0;
516 int uncommonTrapStubCount = 0;
517 int bufferBlobCount = 0;
518 int total = 0;
519 int nmethodAlive = 0;
520 int nmethodNotEntrant = 0;
521 int nmethodZombie = 0;
522 int nmethodUnloaded = 0;
523 int nmethodJava = 0;
524 int nmethodNative = 0;
525 int maxCodeSize = 0;
526 ResourceMark rm;
527
528 CodeBlob *cb;
529 for (cb = first(); cb != NULL; cb = next(cb)) {
530 total++;
531 if (cb->is_nmethod()) {
532 nmethod* nm = (nmethod*)cb;
533
534 if (Verbose && nm->method() != NULL) {
535 ResourceMark rm;
536 char *method_name = nm->method()->name_and_sig_as_C_string();
537 tty->print("%s", method_name);
538 if(nm->is_alive()) { tty->print_cr(" alive"); }
539 if(nm->is_not_entrant()) { tty->print_cr(" not-entrant"); }
540 if(nm->is_zombie()) { tty->print_cr(" zombie"); }
541 }
542
543 nmethodCount++;
544
545 if(nm->is_alive()) { nmethodAlive++; }
546 if(nm->is_not_entrant()) { nmethodNotEntrant++; }
547 if(nm->is_zombie()) { nmethodZombie++; }
548 if(nm->is_unloaded()) { nmethodUnloaded++; }
549 if(nm->is_native_method()) { nmethodNative++; }
550
551 if(nm->method() != NULL && nm->is_java_method()) {
552 nmethodJava++;
553 if(nm->code_size() > maxCodeSize) {
554 maxCodeSize = nm->code_size();
555 }
556 }
557 } else if (cb->is_runtime_stub()) {
558 runtimeStubCount++;
559 } else if (cb->is_deoptimization_stub()) {
560 deoptimizationStubCount++;
561 } else if (cb->is_uncommon_trap_stub()) {
562 uncommonTrapStubCount++;
563 } else if (cb->is_adapter_blob()) {
564 adapterCount++;
565 } else if (cb->is_buffer_blob()) {
566 bufferBlobCount++;
567 }
568 }
569
570 int bucketSize = 512;
571 int bucketLimit = maxCodeSize / bucketSize + 1;
572 int *buckets = NEW_C_HEAP_ARRAY(int, bucketLimit);
573 memset(buckets,0,sizeof(int) * bucketLimit);
574
575 for (cb = first(); cb != NULL; cb = next(cb)) {
576 if (cb->is_nmethod()) {
577 nmethod* nm = (nmethod*)cb;
578 if(nm->is_java_method()) {
579 buckets[nm->code_size() / bucketSize]++;
580 }
581 }
582 }
583 tty->print_cr("Code Cache Entries (total of %d)",total);
584 tty->print_cr("-------------------------------------------------");
585 tty->print_cr("nmethods: %d",nmethodCount);
586 tty->print_cr("\talive: %d",nmethodAlive);
587 tty->print_cr("\tnot_entrant: %d",nmethodNotEntrant);
588 tty->print_cr("\tzombie: %d",nmethodZombie);
589 tty->print_cr("\tunloaded: %d",nmethodUnloaded);
590 tty->print_cr("\tjava: %d",nmethodJava);
591 tty->print_cr("\tnative: %d",nmethodNative);
592 tty->print_cr("runtime_stubs: %d",runtimeStubCount);
593 tty->print_cr("adapters: %d",adapterCount);
594 tty->print_cr("buffer blobs: %d",bufferBlobCount);
595 tty->print_cr("deoptimization_stubs: %d",deoptimizationStubCount);
596 tty->print_cr("uncommon_traps: %d",uncommonTrapStubCount);
597 tty->print_cr("\nnmethod size distribution (non-zombie java)");
598 tty->print_cr("-------------------------------------------------");
599
600 for(int i=0; i<bucketLimit; i++) {
601 if(buckets[i] != 0) {
602 tty->print("%d - %d bytes",i*bucketSize,(i+1)*bucketSize);
603 tty->fill_to(40);
604 tty->print_cr("%d",buckets[i]);
605 }
606 }
607
608 FREE_C_HEAP_ARRAY(int, buckets);
609 }
610
611 void CodeCache::print() {
612 CodeBlob_sizes live;
613 CodeBlob_sizes dead;
614
615 FOR_ALL_BLOBS(p) {
616 if (!p->is_alive()) {
617 dead.add(p);
618 } else {
619 live.add(p);
620 }
621 }
622
623 tty->print_cr("CodeCache:");
624
625 tty->print_cr("nmethod dependency checking time %f", dependentCheckTime.seconds(),
626 dependentCheckTime.seconds() / dependentCheckCount);
627
628 if (!live.is_empty()) {
629 live.print("live");
630 }
631 if (!dead.is_empty()) {
632 dead.print("dead");
633 }
634
635
636 if (Verbose) {
637 // print the oop_map usage
638 int code_size = 0;
639 int number_of_blobs = 0;
640 int number_of_oop_maps = 0;
641 int map_size = 0;
642 FOR_ALL_BLOBS(p) {
643 if (p->is_alive()) {
644 number_of_blobs++;
645 code_size += p->instructions_size();
646 OopMapSet* set = p->oop_maps();
647 if (set != NULL) {
648 number_of_oop_maps += set->size();
649 map_size += set->heap_size();
650 }
651 }
652 }
653 tty->print_cr("OopMaps");
654 tty->print_cr(" #blobs = %d", number_of_blobs);
655 tty->print_cr(" code size = %d", code_size);
656 tty->print_cr(" #oop_maps = %d", number_of_oop_maps);
657 tty->print_cr(" map size = %d", map_size);
658 }
659
660 }
661
662 #endif // PRODUCT