comparison src/share/vm/memory/metaspace.cpp @ 12057:58fc8e2b7b6d

Merge
author Gilles Duboscq <duboscq@ssw.jku.at>
date Thu, 10 Oct 2013 17:22:25 +0200
parents 740e263c80c6
children 1a8fb39bdbc4
comparison
equal deleted inserted replaced
12041:7e5cf369559f 12057:58fc8e2b7b6d
33 #include "memory/metaspace.hpp" 33 #include "memory/metaspace.hpp"
34 #include "memory/metaspaceShared.hpp" 34 #include "memory/metaspaceShared.hpp"
35 #include "memory/resourceArea.hpp" 35 #include "memory/resourceArea.hpp"
36 #include "memory/universe.hpp" 36 #include "memory/universe.hpp"
37 #include "runtime/globals.hpp" 37 #include "runtime/globals.hpp"
38 #include "runtime/java.hpp"
38 #include "runtime/mutex.hpp" 39 #include "runtime/mutex.hpp"
39 #include "runtime/orderAccess.hpp" 40 #include "runtime/orderAccess.hpp"
40 #include "services/memTracker.hpp" 41 #include "services/memTracker.hpp"
41 #include "utilities/copy.hpp" 42 #include "utilities/copy.hpp"
42 #include "utilities/debug.hpp" 43 #include "utilities/debug.hpp"
51 const uint metadata_deallocate_a_lot_block = 10; 52 const uint metadata_deallocate_a_lot_block = 10;
52 const uint metadata_deallocate_a_lock_chunk = 3; 53 const uint metadata_deallocate_a_lock_chunk = 3;
53 size_t const allocation_from_dictionary_limit = 64 * K; 54 size_t const allocation_from_dictionary_limit = 64 * K;
54 55
55 MetaWord* last_allocated = 0; 56 MetaWord* last_allocated = 0;
57
58 size_t Metaspace::_class_metaspace_size;
56 59
57 // Used in declarations in SpaceManager and ChunkManager 60 // Used in declarations in SpaceManager and ChunkManager
58 enum ChunkIndex { 61 enum ChunkIndex {
59 ZeroIndex = 0, 62 ZeroIndex = 0,
60 SpecializedIndex = ZeroIndex, 63 SpecializedIndex = ZeroIndex,
259 VirtualSpace _virtual_space; 262 VirtualSpace _virtual_space;
260 MetaWord* _top; 263 MetaWord* _top;
261 // count of chunks contained in this VirtualSpace 264 // count of chunks contained in this VirtualSpace
262 uintx _container_count; 265 uintx _container_count;
263 266
264 // Convenience functions for logical bottom and end
265 MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
266 MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
267
268 // Convenience functions to access the _virtual_space 267 // Convenience functions to access the _virtual_space
269 char* low() const { return virtual_space()->low(); } 268 char* low() const { return virtual_space()->low(); }
270 char* high() const { return virtual_space()->high(); } 269 char* high() const { return virtual_space()->high(); }
271 270
272 // The first Metachunk will be allocated at the bottom of the 271 // The first Metachunk will be allocated at the bottom of the
281 public: 280 public:
282 281
283 VirtualSpaceNode(size_t byte_size); 282 VirtualSpaceNode(size_t byte_size);
284 VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {} 283 VirtualSpaceNode(ReservedSpace rs) : _top(NULL), _next(NULL), _rs(rs), _container_count(0) {}
285 ~VirtualSpaceNode(); 284 ~VirtualSpaceNode();
285
286 // Convenience functions for logical bottom and end
287 MetaWord* bottom() const { return (MetaWord*) _virtual_space.low(); }
288 MetaWord* end() const { return (MetaWord*) _virtual_space.high(); }
286 289
287 // address of next available space in _virtual_space; 290 // address of next available space in _virtual_space;
288 // Accessors 291 // Accessors
289 VirtualSpaceNode* next() { return _next; } 292 VirtualSpaceNode* next() { return _next; }
290 void set_next(VirtualSpaceNode* v) { _next = v; } 293 void set_next(VirtualSpaceNode* v) { _next = v; }
1311 } 1314 }
1312 } 1315 }
1313 1316
1314 // Class virtual space should always be expanded. Call GC for the other 1317 // Class virtual space should always be expanded. Call GC for the other
1315 // metadata virtual space. 1318 // metadata virtual space.
1316 if (vsl == Metaspace::class_space_list()) return true; 1319 if (Metaspace::using_class_space() &&
1320 (vsl == Metaspace::class_space_list())) return true;
1317 1321
1318 // If this is part of an allocation after a GC, expand 1322 // If this is part of an allocation after a GC, expand
1319 // unconditionally. 1323 // unconditionally.
1320 if (MetaspaceGC::expand_after_GC()) { 1324 if (MetaspaceGC::expand_after_GC()) {
1321 return true; 1325 return true;
2255 void SpaceManager::deallocate(MetaWord* p, size_t word_size) { 2259 void SpaceManager::deallocate(MetaWord* p, size_t word_size) {
2256 assert_lock_strong(_lock); 2260 assert_lock_strong(_lock);
2257 size_t raw_word_size = get_raw_word_size(word_size); 2261 size_t raw_word_size = get_raw_word_size(word_size);
2258 size_t min_size = TreeChunk<Metablock, FreeList>::min_size(); 2262 size_t min_size = TreeChunk<Metablock, FreeList>::min_size();
2259 assert(raw_word_size >= min_size, 2263 assert(raw_word_size >= min_size,
2260 err_msg("Should not deallocate dark matter " SIZE_FORMAT, word_size)); 2264 err_msg("Should not deallocate dark matter " SIZE_FORMAT "<" SIZE_FORMAT, word_size, min_size));
2261 block_freelists()->return_block(p, raw_word_size); 2265 block_freelists()->return_block(p, raw_word_size);
2262 } 2266 }
2263 2267
2264 // Adds a chunk to the list of chunks in use. 2268 // Adds a chunk to the list of chunks in use.
2265 void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) { 2269 void SpaceManager::add_chunk(Metachunk* new_chunk, bool make_current) {
2372 } 2376 }
2373 2377
2374 if (result == NULL) { 2378 if (result == NULL) {
2375 result = grow_and_allocate(word_size); 2379 result = grow_and_allocate(word_size);
2376 } 2380 }
2377 if (result > 0) { 2381 if (result != 0) {
2378 inc_used_metrics(word_size); 2382 inc_used_metrics(word_size);
2379 assert(result != (MetaWord*) chunks_in_use(MediumIndex), 2383 assert(result != (MetaWord*) chunks_in_use(MediumIndex),
2380 "Head of the list is being allocated"); 2384 "Head of the list is being allocated");
2381 } 2385 }
2382 2386
2476 size_t MetaspaceAux::_allocated_capacity_words[] = {0, 0}; 2480 size_t MetaspaceAux::_allocated_capacity_words[] = {0, 0};
2477 size_t MetaspaceAux::_allocated_used_words[] = {0, 0}; 2481 size_t MetaspaceAux::_allocated_used_words[] = {0, 0};
2478 2482
2479 size_t MetaspaceAux::free_bytes() { 2483 size_t MetaspaceAux::free_bytes() {
2480 size_t result = 0; 2484 size_t result = 0;
2481 if (Metaspace::class_space_list() != NULL) { 2485 if (Metaspace::using_class_space() &&
2486 (Metaspace::class_space_list() != NULL)) {
2482 result = result + Metaspace::class_space_list()->free_bytes(); 2487 result = result + Metaspace::class_space_list()->free_bytes();
2483 } 2488 }
2484 if (Metaspace::space_list() != NULL) { 2489 if (Metaspace::space_list() != NULL) {
2485 result = result + Metaspace::space_list()->free_bytes(); 2490 result = result + Metaspace::space_list()->free_bytes();
2486 } 2491 }
2547 } 2552 }
2548 return free * BytesPerWord; 2553 return free * BytesPerWord;
2549 } 2554 }
2550 2555
2551 size_t MetaspaceAux::capacity_bytes_slow(Metaspace::MetadataType mdtype) { 2556 size_t MetaspaceAux::capacity_bytes_slow(Metaspace::MetadataType mdtype) {
2557 if ((mdtype == Metaspace::ClassType) && !Metaspace::using_class_space()) {
2558 return 0;
2559 }
2552 // Don't count the space in the freelists. That space will be 2560 // Don't count the space in the freelists. That space will be
2553 // added to the capacity calculation as needed. 2561 // added to the capacity calculation as needed.
2554 size_t capacity = 0; 2562 size_t capacity = 0;
2555 ClassLoaderDataGraphMetaspaceIterator iter; 2563 ClassLoaderDataGraphMetaspaceIterator iter;
2556 while (iter.repeat()) { 2564 while (iter.repeat()) {
2561 } 2569 }
2562 return capacity * BytesPerWord; 2570 return capacity * BytesPerWord;
2563 } 2571 }
2564 2572
2565 size_t MetaspaceAux::reserved_in_bytes(Metaspace::MetadataType mdtype) { 2573 size_t MetaspaceAux::reserved_in_bytes(Metaspace::MetadataType mdtype) {
2566 size_t reserved = (mdtype == Metaspace::ClassType) ? 2574 if (mdtype == Metaspace::ClassType) {
2567 Metaspace::class_space_list()->virtual_space_total() : 2575 return Metaspace::using_class_space() ?
2568 Metaspace::space_list()->virtual_space_total(); 2576 Metaspace::class_space_list()->virtual_space_total() * BytesPerWord : 0;
2569 return reserved * BytesPerWord; 2577 } else {
2578 return Metaspace::space_list()->virtual_space_total() * BytesPerWord;
2579 }
2570 } 2580 }
2571 2581
2572 size_t MetaspaceAux::min_chunk_size() { return Metaspace::first_chunk_word_size(); } 2582 size_t MetaspaceAux::min_chunk_size() { return Metaspace::first_chunk_word_size(); }
2573 2583
2574 size_t MetaspaceAux::free_chunks_total(Metaspace::MetadataType mdtype) { 2584 size_t MetaspaceAux::free_chunks_total(Metaspace::MetadataType mdtype) {
2585 if ((mdtype == Metaspace::ClassType) && !Metaspace::using_class_space()) {
2586 return 0;
2587 }
2575 ChunkManager* chunk = (mdtype == Metaspace::ClassType) ? 2588 ChunkManager* chunk = (mdtype == Metaspace::ClassType) ?
2576 Metaspace::class_space_list()->chunk_manager() : 2589 Metaspace::class_space_list()->chunk_manager() :
2577 Metaspace::space_list()->chunk_manager(); 2590 Metaspace::space_list()->chunk_manager();
2578 chunk->slow_verify(); 2591 chunk->slow_verify();
2579 return chunk->free_chunks_total(); 2592 return chunk->free_chunks_total();
2580 } 2593 }
2581 2594
2582 size_t MetaspaceAux::free_chunks_total_in_bytes(Metaspace::MetadataType mdtype) { 2595 size_t MetaspaceAux::free_chunks_total_in_bytes(Metaspace::MetadataType mdtype) {
2613 gclog_or_tty->print("]"); 2626 gclog_or_tty->print("]");
2614 } 2627 }
2615 2628
2616 // This is printed when PrintGCDetails 2629 // This is printed when PrintGCDetails
2617 void MetaspaceAux::print_on(outputStream* out) { 2630 void MetaspaceAux::print_on(outputStream* out) {
2618 Metaspace::MetadataType ct = Metaspace::ClassType;
2619 Metaspace::MetadataType nct = Metaspace::NonClassType; 2631 Metaspace::MetadataType nct = Metaspace::NonClassType;
2620 2632
2621 out->print_cr(" Metaspace total " 2633 out->print_cr(" Metaspace total "
2622 SIZE_FORMAT "K, used " SIZE_FORMAT "K," 2634 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
2623 " reserved " SIZE_FORMAT "K", 2635 " reserved " SIZE_FORMAT "K",
2627 SIZE_FORMAT "K, used " SIZE_FORMAT "K," 2639 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
2628 " reserved " SIZE_FORMAT "K", 2640 " reserved " SIZE_FORMAT "K",
2629 allocated_capacity_bytes(nct)/K, 2641 allocated_capacity_bytes(nct)/K,
2630 allocated_used_bytes(nct)/K, 2642 allocated_used_bytes(nct)/K,
2631 reserved_in_bytes(nct)/K); 2643 reserved_in_bytes(nct)/K);
2632 out->print_cr(" class space " 2644 if (Metaspace::using_class_space()) {
2633 SIZE_FORMAT "K, used " SIZE_FORMAT "K," 2645 Metaspace::MetadataType ct = Metaspace::ClassType;
2634 " reserved " SIZE_FORMAT "K", 2646 out->print_cr(" class space "
2635 allocated_capacity_bytes(ct)/K, 2647 SIZE_FORMAT "K, used " SIZE_FORMAT "K,"
2636 allocated_used_bytes(ct)/K, 2648 " reserved " SIZE_FORMAT "K",
2637 reserved_in_bytes(ct)/K); 2649 allocated_capacity_bytes(ct)/K,
2650 allocated_used_bytes(ct)/K,
2651 reserved_in_bytes(ct)/K);
2652 }
2638 } 2653 }
2639 2654
2640 // Print information for class space and data space separately. 2655 // Print information for class space and data space separately.
2641 // This is almost the same as above. 2656 // This is almost the same as above.
2642 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) { 2657 void MetaspaceAux::print_on(outputStream* out, Metaspace::MetadataType mdtype) {
2657 capacity_bytes / K); 2672 capacity_bytes / K);
2658 // Accounting can only be correct if we got the values during a safepoint 2673 // Accounting can only be correct if we got the values during a safepoint
2659 assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong"); 2674 assert(!SafepointSynchronize::is_at_safepoint() || used_and_free == capacity_bytes, "Accounting is wrong");
2660 } 2675 }
2661 2676
2662 // Print total fragmentation for class and data metaspaces separately 2677 // Print total fragmentation for class metaspaces
2678 void MetaspaceAux::print_class_waste(outputStream* out) {
2679 assert(Metaspace::using_class_space(), "class metaspace not used");
2680 size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0;
2681 size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_humongous_count = 0;
2682 ClassLoaderDataGraphMetaspaceIterator iter;
2683 while (iter.repeat()) {
2684 Metaspace* msp = iter.get_next();
2685 if (msp != NULL) {
2686 cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
2687 cls_specialized_count += msp->class_vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
2688 cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex);
2689 cls_small_count += msp->class_vsm()->sum_count_in_chunks_in_use(SmallIndex);
2690 cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex);
2691 cls_medium_count += msp->class_vsm()->sum_count_in_chunks_in_use(MediumIndex);
2692 cls_humongous_count += msp->class_vsm()->sum_count_in_chunks_in_use(HumongousIndex);
2693 }
2694 }
2695 out->print_cr(" class: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
2696 SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
2697 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
2698 "large count " SIZE_FORMAT,
2699 cls_specialized_count, cls_specialized_waste,
2700 cls_small_count, cls_small_waste,
2701 cls_medium_count, cls_medium_waste, cls_humongous_count);
2702 }
2703
2704 // Print total fragmentation for data and class metaspaces separately
2663 void MetaspaceAux::print_waste(outputStream* out) { 2705 void MetaspaceAux::print_waste(outputStream* out) {
2664
2665 size_t specialized_waste = 0, small_waste = 0, medium_waste = 0; 2706 size_t specialized_waste = 0, small_waste = 0, medium_waste = 0;
2666 size_t specialized_count = 0, small_count = 0, medium_count = 0, humongous_count = 0; 2707 size_t specialized_count = 0, small_count = 0, medium_count = 0, humongous_count = 0;
2667 size_t cls_specialized_waste = 0, cls_small_waste = 0, cls_medium_waste = 0;
2668 size_t cls_specialized_count = 0, cls_small_count = 0, cls_medium_count = 0, cls_humongous_count = 0;
2669 2708
2670 ClassLoaderDataGraphMetaspaceIterator iter; 2709 ClassLoaderDataGraphMetaspaceIterator iter;
2671 while (iter.repeat()) { 2710 while (iter.repeat()) {
2672 Metaspace* msp = iter.get_next(); 2711 Metaspace* msp = iter.get_next();
2673 if (msp != NULL) { 2712 if (msp != NULL) {
2676 small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex); 2715 small_waste += msp->vsm()->sum_waste_in_chunks_in_use(SmallIndex);
2677 small_count += msp->vsm()->sum_count_in_chunks_in_use(SmallIndex); 2716 small_count += msp->vsm()->sum_count_in_chunks_in_use(SmallIndex);
2678 medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex); 2717 medium_waste += msp->vsm()->sum_waste_in_chunks_in_use(MediumIndex);
2679 medium_count += msp->vsm()->sum_count_in_chunks_in_use(MediumIndex); 2718 medium_count += msp->vsm()->sum_count_in_chunks_in_use(MediumIndex);
2680 humongous_count += msp->vsm()->sum_count_in_chunks_in_use(HumongousIndex); 2719 humongous_count += msp->vsm()->sum_count_in_chunks_in_use(HumongousIndex);
2681
2682 cls_specialized_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SpecializedIndex);
2683 cls_specialized_count += msp->class_vsm()->sum_count_in_chunks_in_use(SpecializedIndex);
2684 cls_small_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(SmallIndex);
2685 cls_small_count += msp->class_vsm()->sum_count_in_chunks_in_use(SmallIndex);
2686 cls_medium_waste += msp->class_vsm()->sum_waste_in_chunks_in_use(MediumIndex);
2687 cls_medium_count += msp->class_vsm()->sum_count_in_chunks_in_use(MediumIndex);
2688 cls_humongous_count += msp->class_vsm()->sum_count_in_chunks_in_use(HumongousIndex);
2689 } 2720 }
2690 } 2721 }
2691 out->print_cr("Total fragmentation waste (words) doesn't count free space"); 2722 out->print_cr("Total fragmentation waste (words) doesn't count free space");
2692 out->print_cr(" data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", " 2723 out->print_cr(" data: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", "
2693 SIZE_FORMAT " small(s) " SIZE_FORMAT ", " 2724 SIZE_FORMAT " small(s) " SIZE_FORMAT ", "
2694 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", " 2725 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", "
2695 "large count " SIZE_FORMAT, 2726 "large count " SIZE_FORMAT,
2696 specialized_count, specialized_waste, small_count, 2727 specialized_count, specialized_waste, small_count,
2697 small_waste, medium_count, medium_waste, humongous_count); 2728 small_waste, medium_count, medium_waste, humongous_count);
2698 out->print_cr(" class: " SIZE_FORMAT " specialized(s) " SIZE_FORMAT ", " 2729 if (Metaspace::using_class_space()) {
2699 SIZE_FORMAT " small(s) " SIZE_FORMAT ", " 2730 print_class_waste(out);
2700 SIZE_FORMAT " medium(s) " SIZE_FORMAT ", " 2731 }
2701 "large count " SIZE_FORMAT,
2702 cls_specialized_count, cls_specialized_waste,
2703 cls_small_count, cls_small_waste,
2704 cls_medium_count, cls_medium_waste, cls_humongous_count);
2705 } 2732 }
2706 2733
2707 // Dump global metaspace things from the end of ClassLoaderDataGraph 2734 // Dump global metaspace things from the end of ClassLoaderDataGraph
2708 void MetaspaceAux::dump(outputStream* out) { 2735 void MetaspaceAux::dump(outputStream* out) {
2709 out->print_cr("All Metaspace:"); 2736 out->print_cr("All Metaspace:");
2712 print_waste(out); 2739 print_waste(out);
2713 } 2740 }
2714 2741
2715 void MetaspaceAux::verify_free_chunks() { 2742 void MetaspaceAux::verify_free_chunks() {
2716 Metaspace::space_list()->chunk_manager()->verify(); 2743 Metaspace::space_list()->chunk_manager()->verify();
2717 Metaspace::class_space_list()->chunk_manager()->verify(); 2744 if (Metaspace::using_class_space()) {
2745 Metaspace::class_space_list()->chunk_manager()->verify();
2746 }
2718 } 2747 }
2719 2748
2720 void MetaspaceAux::verify_capacity() { 2749 void MetaspaceAux::verify_capacity() {
2721 #ifdef ASSERT 2750 #ifdef ASSERT
2722 size_t running_sum_capacity_bytes = allocated_capacity_bytes(); 2751 size_t running_sum_capacity_bytes = allocated_capacity_bytes();
2774 initialize(lock, type); 2803 initialize(lock, type);
2775 } 2804 }
2776 2805
2777 Metaspace::~Metaspace() { 2806 Metaspace::~Metaspace() {
2778 delete _vsm; 2807 delete _vsm;
2779 delete _class_vsm; 2808 if (using_class_space()) {
2809 delete _class_vsm;
2810 }
2780 } 2811 }
2781 2812
2782 VirtualSpaceList* Metaspace::_space_list = NULL; 2813 VirtualSpaceList* Metaspace::_space_list = NULL;
2783 VirtualSpaceList* Metaspace::_class_space_list = NULL; 2814 VirtualSpaceList* Metaspace::_class_space_list = NULL;
2784 2815
2785 #define VIRTUALSPACEMULTIPLIER 2 2816 #define VIRTUALSPACEMULTIPLIER 2
2817
2818 #ifdef _LP64
2819 void Metaspace::set_narrow_klass_base_and_shift(address metaspace_base, address cds_base) {
2820 // Figure out the narrow_klass_base and the narrow_klass_shift. The
2821 // narrow_klass_base is the lower of the metaspace base and the cds base
2822 // (if cds is enabled). The narrow_klass_shift depends on the distance
2823 // between the lower base and higher address.
2824 address lower_base;
2825 address higher_address;
2826 if (UseSharedSpaces) {
2827 higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
2828 (address)(metaspace_base + class_metaspace_size()));
2829 lower_base = MIN2(metaspace_base, cds_base);
2830 } else {
2831 higher_address = metaspace_base + class_metaspace_size();
2832 lower_base = metaspace_base;
2833 }
2834 Universe::set_narrow_klass_base(lower_base);
2835 if ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint) {
2836 Universe::set_narrow_klass_shift(0);
2837 } else {
2838 assert(!UseSharedSpaces, "Cannot shift with UseSharedSpaces");
2839 Universe::set_narrow_klass_shift(LogKlassAlignmentInBytes);
2840 }
2841 }
2842
2843 // Return TRUE if the specified metaspace_base and cds_base are close enough
2844 // to work with compressed klass pointers.
2845 bool Metaspace::can_use_cds_with_metaspace_addr(char* metaspace_base, address cds_base) {
2846 assert(cds_base != 0 && UseSharedSpaces, "Only use with CDS");
2847 assert(UseCompressedKlassPointers, "Only use with CompressedKlassPtrs");
2848 address lower_base = MIN2((address)metaspace_base, cds_base);
2849 address higher_address = MAX2((address)(cds_base + FileMapInfo::shared_spaces_size()),
2850 (address)(metaspace_base + class_metaspace_size()));
2851 return ((uint64_t)(higher_address - lower_base) < (uint64_t)max_juint);
2852 }
2853
2854 // Try to allocate the metaspace at the requested addr.
2855 void Metaspace::allocate_metaspace_compressed_klass_ptrs(char* requested_addr, address cds_base) {
2856 assert(using_class_space(), "called improperly");
2857 assert(UseCompressedKlassPointers, "Only use with CompressedKlassPtrs");
2858 assert(class_metaspace_size() < KlassEncodingMetaspaceMax,
2859 "Metaspace size is too big");
2860
2861 ReservedSpace metaspace_rs = ReservedSpace(class_metaspace_size(),
2862 os::vm_allocation_granularity(),
2863 false, requested_addr, 0);
2864 if (!metaspace_rs.is_reserved()) {
2865 if (UseSharedSpaces) {
2866 // Keep trying to allocate the metaspace, increasing the requested_addr
2867 // by 1GB each time, until we reach an address that will no longer allow
2868 // use of CDS with compressed klass pointers.
2869 char *addr = requested_addr;
2870 while (!metaspace_rs.is_reserved() && (addr + 1*G > addr) &&
2871 can_use_cds_with_metaspace_addr(addr + 1*G, cds_base)) {
2872 addr = addr + 1*G;
2873 metaspace_rs = ReservedSpace(class_metaspace_size(),
2874 os::vm_allocation_granularity(), false, addr, 0);
2875 }
2876 }
2877
2878 // If no successful allocation then try to allocate the space anywhere. If
2879 // that fails then OOM doom. At this point we cannot try allocating the
2880 // metaspace as if UseCompressedKlassPointers is off because too much
2881 // initialization has happened that depends on UseCompressedKlassPointers.
2882 // So, UseCompressedKlassPointers cannot be turned off at this point.
2883 if (!metaspace_rs.is_reserved()) {
2884 metaspace_rs = ReservedSpace(class_metaspace_size(),
2885 os::vm_allocation_granularity(), false);
2886 if (!metaspace_rs.is_reserved()) {
2887 vm_exit_during_initialization(err_msg("Could not allocate metaspace: %d bytes",
2888 class_metaspace_size()));
2889 }
2890 }
2891 }
2892
2893 // If we got here then the metaspace got allocated.
2894 MemTracker::record_virtual_memory_type((address)metaspace_rs.base(), mtClass);
2895
2896 // Verify that we can use shared spaces. Otherwise, turn off CDS.
2897 if (UseSharedSpaces && !can_use_cds_with_metaspace_addr(metaspace_rs.base(), cds_base)) {
2898 FileMapInfo::stop_sharing_and_unmap(
2899 "Could not allocate metaspace at a compatible address");
2900 }
2901
2902 set_narrow_klass_base_and_shift((address)metaspace_rs.base(),
2903 UseSharedSpaces ? (address)cds_base : 0);
2904
2905 initialize_class_space(metaspace_rs);
2906
2907 if (PrintCompressedOopsMode || (PrintMiscellaneous && Verbose)) {
2908 gclog_or_tty->print_cr("Narrow klass base: " PTR_FORMAT ", Narrow klass shift: " SIZE_FORMAT,
2909 Universe::narrow_klass_base(), Universe::narrow_klass_shift());
2910 gclog_or_tty->print_cr("Metaspace Size: " SIZE_FORMAT " Address: " PTR_FORMAT " Req Addr: " PTR_FORMAT,
2911 class_metaspace_size(), metaspace_rs.base(), requested_addr);
2912 }
2913 }
2914
2915 // For UseCompressedKlassPointers the class space is reserved above the top of
2916 // the Java heap. The argument passed in is at the base of the compressed space.
2917 void Metaspace::initialize_class_space(ReservedSpace rs) {
2918 // The reserved space size may be bigger because of alignment, esp with UseLargePages
2919 assert(rs.size() >= ClassMetaspaceSize,
2920 err_msg(SIZE_FORMAT " != " UINTX_FORMAT, rs.size(), ClassMetaspaceSize));
2921 assert(using_class_space(), "Must be using class space");
2922 _class_space_list = new VirtualSpaceList(rs);
2923 }
2924
2925 #endif
2786 2926
2787 void Metaspace::global_initialize() { 2927 void Metaspace::global_initialize() {
2788 // Initialize the alignment for shared spaces. 2928 // Initialize the alignment for shared spaces.
2789 int max_alignment = os::vm_page_size(); 2929 int max_alignment = os::vm_page_size();
2930 size_t cds_total = 0;
2931
2932 set_class_metaspace_size(align_size_up(ClassMetaspaceSize,
2933 os::vm_allocation_granularity()));
2934
2790 MetaspaceShared::set_max_alignment(max_alignment); 2935 MetaspaceShared::set_max_alignment(max_alignment);
2791 2936
2792 if (DumpSharedSpaces) { 2937 if (DumpSharedSpaces) {
2793 SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment); 2938 SharedReadOnlySize = align_size_up(SharedReadOnlySize, max_alignment);
2794 SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment); 2939 SharedReadWriteSize = align_size_up(SharedReadWriteSize, max_alignment);
2796 SharedMiscCodeSize = align_size_up(SharedMiscCodeSize, max_alignment); 2941 SharedMiscCodeSize = align_size_up(SharedMiscCodeSize, max_alignment);
2797 2942
2798 // Initialize with the sum of the shared space sizes. The read-only 2943 // Initialize with the sum of the shared space sizes. The read-only
2799 // and read write metaspace chunks will be allocated out of this and the 2944 // and read write metaspace chunks will be allocated out of this and the
2800 // remainder is the misc code and data chunks. 2945 // remainder is the misc code and data chunks.
2801 size_t total = align_size_up(SharedReadOnlySize + SharedReadWriteSize + 2946 cds_total = FileMapInfo::shared_spaces_size();
2802 SharedMiscDataSize + SharedMiscCodeSize, 2947 _space_list = new VirtualSpaceList(cds_total/wordSize);
2803 os::vm_allocation_granularity()); 2948
2804 size_t word_size = total/wordSize; 2949 #ifdef _LP64
2805 _space_list = new VirtualSpaceList(word_size); 2950 // Set the compressed klass pointer base so that decoding of these pointers works
2951 // properly when creating the shared archive.
2952 assert(UseCompressedOops && UseCompressedKlassPointers,
2953 "UseCompressedOops and UseCompressedKlassPointers must be set");
2954 Universe::set_narrow_klass_base((address)_space_list->current_virtual_space()->bottom());
2955 if (TraceMetavirtualspaceAllocation && Verbose) {
2956 gclog_or_tty->print_cr("Setting_narrow_klass_base to Address: " PTR_FORMAT,
2957 _space_list->current_virtual_space()->bottom());
2958 }
2959
2960 // Set the shift to zero.
2961 assert(class_metaspace_size() < (uint64_t)(max_juint) - cds_total,
2962 "CDS region is too large");
2963 Universe::set_narrow_klass_shift(0);
2964 #endif
2965
2806 } else { 2966 } else {
2807 // If using shared space, open the file that contains the shared space 2967 // If using shared space, open the file that contains the shared space
2808 // and map in the memory before initializing the rest of metaspace (so 2968 // and map in the memory before initializing the rest of metaspace (so
2809 // the addresses don't conflict) 2969 // the addresses don't conflict)
2970 address cds_address = NULL;
2810 if (UseSharedSpaces) { 2971 if (UseSharedSpaces) {
2811 FileMapInfo* mapinfo = new FileMapInfo(); 2972 FileMapInfo* mapinfo = new FileMapInfo();
2812 memset(mapinfo, 0, sizeof(FileMapInfo)); 2973 memset(mapinfo, 0, sizeof(FileMapInfo));
2813 2974
2814 // Open the shared archive file, read and validate the header. If 2975 // Open the shared archive file, read and validate the header. If
2819 FileMapInfo::set_current_info(mapinfo); 2980 FileMapInfo::set_current_info(mapinfo);
2820 } else { 2981 } else {
2821 assert(!mapinfo->is_open() && !UseSharedSpaces, 2982 assert(!mapinfo->is_open() && !UseSharedSpaces,
2822 "archive file not closed or shared spaces not disabled."); 2983 "archive file not closed or shared spaces not disabled.");
2823 } 2984 }
2824 } 2985 cds_total = FileMapInfo::shared_spaces_size();
2986 cds_address = (address)mapinfo->region_base(0);
2987 }
2988
2989 #ifdef _LP64
2990 // If UseCompressedKlassPointers is set then allocate the metaspace area
2991 // above the heap and above the CDS area (if it exists).
2992 if (using_class_space()) {
2993 if (UseSharedSpaces) {
2994 allocate_metaspace_compressed_klass_ptrs((char *)(cds_address + cds_total), cds_address);
2995 } else {
2996 allocate_metaspace_compressed_klass_ptrs((char *)CompressedKlassPointersBase, 0);
2997 }
2998 }
2999 #endif
2825 3000
2826 // Initialize these before initializing the VirtualSpaceList 3001 // Initialize these before initializing the VirtualSpaceList
2827 _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord; 3002 _first_chunk_word_size = InitialBootClassLoaderMetaspaceSize / BytesPerWord;
2828 _first_chunk_word_size = align_word_size_up(_first_chunk_word_size); 3003 _first_chunk_word_size = align_word_size_up(_first_chunk_word_size);
2829 // Make the first class chunk bigger than a medium chunk so it's not put 3004 // Make the first class chunk bigger than a medium chunk so it's not put
2838 // Initialize the list of virtual spaces. 3013 // Initialize the list of virtual spaces.
2839 _space_list = new VirtualSpaceList(word_size); 3014 _space_list = new VirtualSpaceList(word_size);
2840 } 3015 }
2841 } 3016 }
2842 3017
2843 // For UseCompressedKlassPointers the class space is reserved as a piece of the 3018 void Metaspace::initialize(Mutex* lock, MetaspaceType type) {
2844 // Java heap because the compression algorithm is the same for each. The
2845 // argument passed in is at the top of the compressed space
2846 void Metaspace::initialize_class_space(ReservedSpace rs) {
2847 // The reserved space size may be bigger because of alignment, esp with UseLargePages
2848 assert(rs.size() >= ClassMetaspaceSize,
2849 err_msg(SIZE_FORMAT " != " UINTX_FORMAT, rs.size(), ClassMetaspaceSize));
2850 _class_space_list = new VirtualSpaceList(rs);
2851 }
2852
2853 void Metaspace::initialize(Mutex* lock,
2854 MetaspaceType type) {
2855 3019
2856 assert(space_list() != NULL, 3020 assert(space_list() != NULL,
2857 "Metadata VirtualSpaceList has not been initialized"); 3021 "Metadata VirtualSpaceList has not been initialized");
2858 3022
2859 _vsm = new SpaceManager(Metaspace::NonClassType, lock, space_list()); 3023 _vsm = new SpaceManager(NonClassType, lock, space_list());
2860 if (_vsm == NULL) { 3024 if (_vsm == NULL) {
2861 return; 3025 return;
2862 } 3026 }
2863 size_t word_size; 3027 size_t word_size;
2864 size_t class_word_size; 3028 size_t class_word_size;
2865 vsm()->get_initial_chunk_sizes(type, 3029 vsm()->get_initial_chunk_sizes(type, &word_size, &class_word_size);
2866 &word_size, 3030
2867 &class_word_size); 3031 if (using_class_space()) {
2868 3032 assert(class_space_list() != NULL,
2869 assert(class_space_list() != NULL, 3033 "Class VirtualSpaceList has not been initialized");
2870 "Class VirtualSpaceList has not been initialized"); 3034
2871 3035 // Allocate SpaceManager for classes.
2872 // Allocate SpaceManager for classes. 3036 _class_vsm = new SpaceManager(ClassType, lock, class_space_list());
2873 _class_vsm = new SpaceManager(Metaspace::ClassType, lock, class_space_list()); 3037 if (_class_vsm == NULL) {
2874 if (_class_vsm == NULL) { 3038 return;
2875 return; 3039 }
2876 } 3040 }
2877 3041
2878 MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag); 3042 MutexLockerEx cl(SpaceManager::expand_lock(), Mutex::_no_safepoint_check_flag);
2879 3043
2880 // Allocate chunk for metadata objects 3044 // Allocate chunk for metadata objects
2886 // Add to this manager's list of chunks in use and current_chunk(). 3050 // Add to this manager's list of chunks in use and current_chunk().
2887 vsm()->add_chunk(new_chunk, true); 3051 vsm()->add_chunk(new_chunk, true);
2888 } 3052 }
2889 3053
2890 // Allocate chunk for class metadata objects 3054 // Allocate chunk for class metadata objects
2891 Metachunk* class_chunk = 3055 if (using_class_space()) {
2892 class_space_list()->get_initialization_chunk(class_word_size, 3056 Metachunk* class_chunk =
2893 class_vsm()->medium_chunk_bunch()); 3057 class_space_list()->get_initialization_chunk(class_word_size,
2894 if (class_chunk != NULL) { 3058 class_vsm()->medium_chunk_bunch());
2895 class_vsm()->add_chunk(class_chunk, true); 3059 if (class_chunk != NULL) {
3060 class_vsm()->add_chunk(class_chunk, true);
3061 }
2896 } 3062 }
2897 3063
2898 _alloc_record_head = NULL; 3064 _alloc_record_head = NULL;
2899 _alloc_record_tail = NULL; 3065 _alloc_record_tail = NULL;
2900 } 3066 }
2904 return ReservedSpace::allocation_align_size_up(byte_size) / wordSize; 3070 return ReservedSpace::allocation_align_size_up(byte_size) / wordSize;
2905 } 3071 }
2906 3072
2907 MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) { 3073 MetaWord* Metaspace::allocate(size_t word_size, MetadataType mdtype) {
2908 // DumpSharedSpaces doesn't use class metadata area (yet) 3074 // DumpSharedSpaces doesn't use class metadata area (yet)
2909 if (mdtype == ClassType && !DumpSharedSpaces) { 3075 // Also, don't use class_vsm() unless UseCompressedKlassPointers is true.
3076 if (mdtype == ClassType && using_class_space()) {
2910 return class_vsm()->allocate(word_size); 3077 return class_vsm()->allocate(word_size);
2911 } else { 3078 } else {
2912 return vsm()->allocate(word_size); 3079 return vsm()->allocate(word_size);
2913 } 3080 }
2914 } 3081 }
2935 assert(DumpSharedSpaces, "only useful and valid for dumping shared spaces"); 3102 assert(DumpSharedSpaces, "only useful and valid for dumping shared spaces");
2936 return (char*)vsm()->current_chunk()->bottom(); 3103 return (char*)vsm()->current_chunk()->bottom();
2937 } 3104 }
2938 3105
2939 size_t Metaspace::used_words_slow(MetadataType mdtype) const { 3106 size_t Metaspace::used_words_slow(MetadataType mdtype) const {
2940 // return vsm()->allocated_used_words(); 3107 if (mdtype == ClassType) {
2941 return mdtype == ClassType ? class_vsm()->sum_used_in_chunks_in_use() : 3108 return using_class_space() ? class_vsm()->sum_used_in_chunks_in_use() : 0;
2942 vsm()->sum_used_in_chunks_in_use(); // includes overhead! 3109 } else {
3110 return vsm()->sum_used_in_chunks_in_use(); // includes overhead!
3111 }
2943 } 3112 }
2944 3113
2945 size_t Metaspace::free_words(MetadataType mdtype) const { 3114 size_t Metaspace::free_words(MetadataType mdtype) const {
2946 return mdtype == ClassType ? class_vsm()->sum_free_in_chunks_in_use() : 3115 if (mdtype == ClassType) {
2947 vsm()->sum_free_in_chunks_in_use(); 3116 return using_class_space() ? class_vsm()->sum_free_in_chunks_in_use() : 0;
3117 } else {
3118 return vsm()->sum_free_in_chunks_in_use();
3119 }
2948 } 3120 }
2949 3121
2950 // Space capacity in the Metaspace. It includes 3122 // Space capacity in the Metaspace. It includes
2951 // space in the list of chunks from which allocations 3123 // space in the list of chunks from which allocations
2952 // have been made. Don't include space in the global freelist and 3124 // have been made. Don't include space in the global freelist and
2953 // in the space available in the dictionary which 3125 // in the space available in the dictionary which
2954 // is already counted in some chunk. 3126 // is already counted in some chunk.
2955 size_t Metaspace::capacity_words_slow(MetadataType mdtype) const { 3127 size_t Metaspace::capacity_words_slow(MetadataType mdtype) const {
2956 return mdtype == ClassType ? class_vsm()->sum_capacity_in_chunks_in_use() : 3128 if (mdtype == ClassType) {
2957 vsm()->sum_capacity_in_chunks_in_use(); 3129 return using_class_space() ? class_vsm()->sum_capacity_in_chunks_in_use() : 0;
3130 } else {
3131 return vsm()->sum_capacity_in_chunks_in_use();
3132 }
2958 } 3133 }
2959 3134
2960 size_t Metaspace::used_bytes_slow(MetadataType mdtype) const { 3135 size_t Metaspace::used_bytes_slow(MetadataType mdtype) const {
2961 return used_words_slow(mdtype) * BytesPerWord; 3136 return used_words_slow(mdtype) * BytesPerWord;
2962 } 3137 }
2975 #ifdef ASSERT 3150 #ifdef ASSERT
2976 Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5); 3151 Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
2977 #endif 3152 #endif
2978 return; 3153 return;
2979 } 3154 }
2980 if (is_class) { 3155 if (is_class && using_class_space()) {
2981 class_vsm()->deallocate(ptr, word_size); 3156 class_vsm()->deallocate(ptr, word_size);
2982 } else { 3157 } else {
2983 vsm()->deallocate(ptr, word_size); 3158 vsm()->deallocate(ptr, word_size);
2984 } 3159 }
2985 } else { 3160 } else {
2986 MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag); 3161 MutexLockerEx ml(vsm()->lock(), Mutex::_no_safepoint_check_flag);
2990 #ifdef ASSERT 3165 #ifdef ASSERT
2991 Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5); 3166 Copy::fill_to_words((HeapWord*)ptr, word_size, 0xf5f5f5f5);
2992 #endif 3167 #endif
2993 return; 3168 return;
2994 } 3169 }
2995 if (is_class) { 3170 if (is_class && using_class_space()) {
2996 class_vsm()->deallocate(ptr, word_size); 3171 class_vsm()->deallocate(ptr, word_size);
2997 } else { 3172 } else {
2998 vsm()->deallocate(ptr, word_size); 3173 vsm()->deallocate(ptr, word_size);
2999 } 3174 }
3000 } 3175 }
3099 3274
3100 void Metaspace::purge() { 3275 void Metaspace::purge() {
3101 MutexLockerEx cl(SpaceManager::expand_lock(), 3276 MutexLockerEx cl(SpaceManager::expand_lock(),
3102 Mutex::_no_safepoint_check_flag); 3277 Mutex::_no_safepoint_check_flag);
3103 space_list()->purge(); 3278 space_list()->purge();
3104 class_space_list()->purge(); 3279 if (using_class_space()) {
3280 class_space_list()->purge();
3281 }
3105 } 3282 }
3106 3283
3107 void Metaspace::print_on(outputStream* out) const { 3284 void Metaspace::print_on(outputStream* out) const {
3108 // Print both class virtual space counts and metaspace. 3285 // Print both class virtual space counts and metaspace.
3109 if (Verbose) { 3286 if (Verbose) {
3110 vsm()->print_on(out); 3287 vsm()->print_on(out);
3288 if (using_class_space()) {
3111 class_vsm()->print_on(out); 3289 class_vsm()->print_on(out);
3290 }
3112 } 3291 }
3113 } 3292 }
3114 3293
3115 bool Metaspace::contains(const void * ptr) { 3294 bool Metaspace::contains(const void * ptr) {
3116 if (MetaspaceShared::is_in_shared_space(ptr)) { 3295 if (MetaspaceShared::is_in_shared_space(ptr)) {
3120 // at the end, the pointer will be in one of them. The virtual spaces 3299 // at the end, the pointer will be in one of them. The virtual spaces
3121 // aren't deleted presently. When they are, some sort of locking might 3300 // aren't deleted presently. When they are, some sort of locking might
3122 // be needed. Note, locking this can cause inversion problems with the 3301 // be needed. Note, locking this can cause inversion problems with the
3123 // caller in MetaspaceObj::is_metadata() function. 3302 // caller in MetaspaceObj::is_metadata() function.
3124 return space_list()->contains(ptr) || 3303 return space_list()->contains(ptr) ||
3125 class_space_list()->contains(ptr); 3304 (using_class_space() && class_space_list()->contains(ptr));
3126 } 3305 }
3127 3306
3128 void Metaspace::verify() { 3307 void Metaspace::verify() {
3129 vsm()->verify(); 3308 vsm()->verify();
3130 class_vsm()->verify(); 3309 if (using_class_space()) {
3310 class_vsm()->verify();
3311 }
3131 } 3312 }
3132 3313
3133 void Metaspace::dump(outputStream* const out) const { 3314 void Metaspace::dump(outputStream* const out) const {
3134 out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm()); 3315 out->print_cr("\nVirtual space manager: " INTPTR_FORMAT, vsm());
3135 vsm()->dump(out); 3316 vsm()->dump(out);
3136 out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm()); 3317 if (using_class_space()) {
3137 class_vsm()->dump(out); 3318 out->print_cr("\nClass space manager: " INTPTR_FORMAT, class_vsm());
3138 } 3319 class_vsm()->dump(out);
3320 }
3321 }