--- old/src/hotspot/os/bsd/os_bsd.cpp 2019-09-30 17:12:51.328030090 -0700 +++ new/src/hotspot/os/bsd/os_bsd.cpp 2019-09-30 17:12:50.948030103 -0700 @@ -2007,6 +2007,10 @@ return 0; } +int os::numa_get_address_id(uintptr_t addr) { + return 0; +} + bool os::get_page_info(char *start, page_info* info) { return false; } --- old/src/hotspot/os/linux/os_linux.cpp 2019-09-30 17:12:52.484030049 -0700 +++ new/src/hotspot/os/linux/os_linux.cpp 2019-09-30 17:12:52.120030062 -0700 @@ -3011,6 +3011,24 @@ return 0; } +int os::numa_get_address_id(uintptr_t addr) { +#ifndef MPOL_F_NODE +#define MPOL_F_NODE (1<<0) // Return next IL mode instead of node mask +#endif + +#ifndef MPOL_F_ADDR +#define MPOL_F_ADDR (1<<1) // Look up VMA using address +#endif + + uint32_t id = (uint32_t)-1; + + if (syscall(SYS_get_mempolicy, &id, NULL, 0, addr, MPOL_F_NODE | MPOL_F_ADDR) == -1) { + warning("Failed to get numa id at " PTR_FORMAT " with errno=%d", p2i((void*)addr), errno); + return os::InvalidId; + } + return id; +} + int os::Linux::get_existing_num_nodes() { int node; int highest_node_number = Linux::numa_max_node(); --- old/src/hotspot/os/solaris/os_solaris.cpp 2019-09-30 17:12:54.712029972 -0700 +++ new/src/hotspot/os/solaris/os_solaris.cpp 2019-09-30 17:12:54.340029985 -0700 @@ -2233,7 +2233,7 @@ char *res = Solaris::mmap_chunk(addr, size, MAP_PRIVATE|MAP_FIXED, prot); if (res != NULL) { if (UseNUMAInterleaving) { - numa_make_global(addr, bytes); + numa_make_global(addr, bytes); } return 0; } @@ -2428,6 +2428,10 @@ return ids[os::random() % r]; } +int os::numa_get_address_id(uintptr_t addr) { + return 0; +} + // Request information about the page. bool os::get_page_info(char *start, page_info* info) { const uint_t info_types[] = { MEMINFO_VLGRP, MEMINFO_VPAGESIZE }; --- old/src/hotspot/os/windows/os_windows.cpp 2019-09-30 17:12:55.916029930 -0700 +++ new/src/hotspot/os/windows/os_windows.cpp 2019-09-30 17:12:55.540029943 -0700 @@ -3450,6 +3450,8 @@ } } +int os::numa_get_address_id(uintptr_t addr) { return 0; } + bool os::get_page_info(char *start, page_info* info) { return false; } --- old/src/hotspot/share/gc/g1/g1AllocRegion.cpp 2019-09-30 17:12:57.116029888 -0700 +++ new/src/hotspot/share/gc/g1/g1AllocRegion.cpp 2019-09-30 17:12:56.752029901 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -250,17 +250,19 @@ #endif // PRODUCT G1AllocRegion::G1AllocRegion(const char* name, - bool bot_updates) + bool bot_updates, + uint node_index) : _alloc_region(NULL), _count(0), _used_bytes_before(0), _bot_updates(bot_updates), - _name(name) + _name(name), + _node_index(node_index) { } HeapRegion* MutatorAllocRegion::allocate_new_region(size_t word_size, bool force) { - return _g1h->new_mutator_alloc_region(word_size, force); + return _g1h->new_mutator_alloc_region(word_size, force, _node_index); } void MutatorAllocRegion::retire_region(HeapRegion* alloc_region, --- old/src/hotspot/share/gc/g1/g1AllocRegion.hpp 2019-09-30 17:12:58.200029850 -0700 +++ new/src/hotspot/share/gc/g1/g1AllocRegion.hpp 2019-09-30 17:12:57.828029863 -0700 @@ -28,6 +28,7 @@ #include "gc/g1/heapRegion.hpp" #include "gc/g1/g1EvacStats.hpp" #include "gc/g1/g1HeapRegionAttr.hpp" +#include "gc/g1/g1MemoryNodeManager.hpp" class G1CollectedHeap; @@ -38,7 +39,7 @@ // and a lock will need to be taken when the active region needs to be // replaced. -class G1AllocRegion { +class G1AllocRegion : public CHeapObj { private: // The active allocating region we are currently allocating out @@ -91,6 +92,9 @@ HeapWord* new_alloc_region_and_allocate(size_t word_size, bool force); protected: + // The memory node index this allocation region belongs to. + uint _node_index; + // Reset the alloc region to point a the dummy region. void reset_alloc_region(); @@ -131,7 +135,7 @@ virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes) = 0; - G1AllocRegion(const char* name, bool bot_updates); + G1AllocRegion(const char* name, bool bot_updates, uint node_index); public: static void setup(G1CollectedHeap* g1h, HeapRegion* dummy_region); @@ -220,8 +224,8 @@ virtual void retire_region(HeapRegion* alloc_region, size_t allocated_bytes); virtual size_t retire(bool fill_up); public: - MutatorAllocRegion() - : G1AllocRegion("Mutator Alloc Region", false /* bot_updates */), + MutatorAllocRegion(uint node_index) + : G1AllocRegion("Mutator Alloc Region", false /* bot_updates */, node_index), _wasted_bytes(0), _retained_alloc_region(NULL) { } @@ -245,6 +249,7 @@ virtual void init(); }; + // Common base class for allocation regions used during GC. class G1GCAllocRegion : public G1AllocRegion { protected: @@ -256,8 +261,9 @@ virtual size_t retire(bool fill_up); - G1GCAllocRegion(const char* name, bool bot_updates, G1EvacStats* stats, G1HeapRegionAttr::region_type_t purpose) - : G1AllocRegion(name, bot_updates), _stats(stats), _purpose(purpose) { + G1GCAllocRegion(const char* name, bool bot_updates, G1EvacStats* stats, + G1HeapRegionAttr::region_type_t purpose, uint node_index = G1MemoryNodeManager::AnyNodeIndex) + : G1AllocRegion(name, bot_updates, node_index), _stats(stats), _purpose(purpose) { assert(stats != NULL, "Must pass non-NULL PLAB statistics"); } }; --- old/src/hotspot/share/gc/g1/g1Allocator.cpp 2019-09-30 17:12:59.308029812 -0700 +++ new/src/hotspot/share/gc/g1/g1Allocator.cpp 2019-09-30 17:12:58.932029825 -0700 @@ -28,6 +28,7 @@ #include "gc/g1/g1EvacStats.inline.hpp" #include "gc/g1/g1EvacuationInfo.hpp" #include "gc/g1/g1CollectedHeap.inline.hpp" +#include "gc/g1/g1NUMA.hpp" #include "gc/g1/g1Policy.hpp" #include "gc/g1/heapRegion.inline.hpp" #include "gc/g1/heapRegionSet.inline.hpp" @@ -38,20 +39,44 @@ _g1h(heap), _survivor_is_full(false), _old_is_full(false), - _mutator_alloc_region(), + _num_alloc_regions(heap->mem_node_mgr()->num_active_nodes()), + _mutator_alloc_regions(NULL), _survivor_gc_alloc_region(heap->alloc_buffer_stats(G1HeapRegionAttr::Young)), _old_gc_alloc_region(heap->alloc_buffer_stats(G1HeapRegionAttr::Old)), _retained_old_gc_alloc_region(NULL) { + + _mutator_alloc_regions = NEW_C_HEAP_ARRAY(MutatorAllocRegion, _num_alloc_regions, mtGC); + for (uint i = 0; i < _num_alloc_regions; i++) { + ::new(_mutator_alloc_regions + i) MutatorAllocRegion(i); + } +} + +G1Allocator::~G1Allocator() { + for (uint i = 0; i < _num_alloc_regions; i++) { + _mutator_alloc_regions[i].~MutatorAllocRegion(); + } + FREE_C_HEAP_ARRAY(MutatorAllocRegion, _mutator_alloc_regions); } -void G1Allocator::init_mutator_alloc_region() { - assert(_mutator_alloc_region.get() == NULL, "pre-condition"); - _mutator_alloc_region.init(); +#ifdef ASSERT +bool G1Allocator::has_mutator_alloc_region() { + uint node_index = current_node_index(); + return mutator_alloc_region(node_index)->get() != NULL; } +#endif -void G1Allocator::release_mutator_alloc_region() { - _mutator_alloc_region.release(); - assert(_mutator_alloc_region.get() == NULL, "post-condition"); +void G1Allocator::init_mutator_alloc_regions() { + for (uint i = 0; i < _num_alloc_regions; i++) { + assert(mutator_alloc_region(i)->get() == NULL, "pre-condition"); + mutator_alloc_region(i)->init(); + } +} + +void G1Allocator::release_mutator_alloc_regions() { + for (uint i = 0; i < _num_alloc_regions; i++) { + mutator_alloc_region(i)->release(); + assert(mutator_alloc_region(i)->get() == NULL, "post-condition"); + } } bool G1Allocator::is_retained_old_region(HeapRegion* hr) { @@ -146,7 +171,8 @@ // since we can't allow tlabs to grow big enough to accommodate // humongous objects. - HeapRegion* hr = mutator_alloc_region()->get(); + uint node_index = current_node_index(); + HeapRegion* hr = mutator_alloc_region(node_index)->get(); size_t max_tlab = _g1h->max_tlab_size() * wordSize; if (hr == NULL) { return max_tlab; @@ -156,8 +182,11 @@ } size_t G1Allocator::used_in_alloc_regions() { - assert(Heap_lock->owner() != NULL, "Should be owned on this thread's behalf."); - return mutator_alloc_region()->used_in_alloc_regions(); + size_t used = 0; + for (uint i = 0; i < _num_alloc_regions; i++) { + used += mutator_alloc_region(i)->used_in_alloc_regions(); + } + return used; } --- old/src/hotspot/share/gc/g1/g1Allocator.hpp 2019-09-30 17:13:00.400029774 -0700 +++ new/src/hotspot/share/gc/g1/g1Allocator.hpp 2019-09-30 17:13:00.028029787 -0700 @@ -44,8 +44,11 @@ bool _survivor_is_full; bool _old_is_full; + // The number of MutatorAllocRegions used, one per memory node. + size_t _num_alloc_regions; + // Alloc region used to satisfy mutator allocation requests. - MutatorAllocRegion _mutator_alloc_region; + MutatorAllocRegion* _mutator_alloc_regions; // Alloc region used to satisfy allocation requests by the GC for // survivor objects. @@ -68,29 +71,34 @@ HeapRegion** retained); // Accessors to the allocation regions. - inline MutatorAllocRegion* mutator_alloc_region(); + inline MutatorAllocRegion* mutator_alloc_region(uint node_index); inline SurvivorGCAllocRegion* survivor_gc_alloc_region(); inline OldGCAllocRegion* old_gc_alloc_region(); // Allocation attempt during GC for a survivor object / PLAB. HeapWord* survivor_attempt_allocation(size_t min_word_size, - size_t desired_word_size, - size_t* actual_word_size); + size_t desired_word_size, + size_t* actual_word_size); // Allocation attempt during GC for an old object / PLAB. HeapWord* old_attempt_allocation(size_t min_word_size, - size_t desired_word_size, - size_t* actual_word_size); + size_t desired_word_size, + size_t* actual_word_size); + + // Node index of current thread. + inline uint current_node_index() const; + public: G1Allocator(G1CollectedHeap* heap); + ~G1Allocator(); #ifdef ASSERT // Do we currently have an active mutator region to allocate into? - bool has_mutator_alloc_region() { return mutator_alloc_region()->get() != NULL; } + bool has_mutator_alloc_region(); #endif - void init_mutator_alloc_region(); - void release_mutator_alloc_region(); + void init_mutator_alloc_regions(); + void release_mutator_alloc_regions(); void init_gc_alloc_regions(G1EvacuationInfo& evacuation_info); void release_gc_alloc_regions(G1EvacuationInfo& evacuation_info); --- old/src/hotspot/share/gc/g1/g1Allocator.inline.hpp 2019-09-30 17:13:01.476029736 -0700 +++ new/src/hotspot/share/gc/g1/g1Allocator.inline.hpp 2019-09-30 17:13:01.104029749 -0700 @@ -30,8 +30,13 @@ #include "gc/shared/plab.inline.hpp" #include "memory/universe.hpp" -inline MutatorAllocRegion* G1Allocator::mutator_alloc_region() { - return &_mutator_alloc_region; +inline uint G1Allocator::current_node_index() const { + return _g1h->mem_node_mgr()->index_of_current_thread(); +} + +inline MutatorAllocRegion* G1Allocator::mutator_alloc_region(uint node_index) { + assert(_g1h->mem_node_mgr()->is_valid_node_index(node_index), "Invariant, index %u", node_index); + return &_mutator_alloc_regions[node_index]; } inline SurvivorGCAllocRegion* G1Allocator::survivor_gc_alloc_region() { @@ -45,22 +50,25 @@ inline HeapWord* G1Allocator::attempt_allocation(size_t min_word_size, size_t desired_word_size, size_t* actual_word_size) { - HeapWord* result = mutator_alloc_region()->attempt_retained_allocation(min_word_size, desired_word_size, actual_word_size); + uint node_index = current_node_index(); + HeapWord* result = mutator_alloc_region(node_index)->attempt_retained_allocation(min_word_size, desired_word_size, actual_word_size); if (result != NULL) { return result; } - return mutator_alloc_region()->attempt_allocation(min_word_size, desired_word_size, actual_word_size); + return mutator_alloc_region(node_index)->attempt_allocation(min_word_size, desired_word_size, actual_word_size); } inline HeapWord* G1Allocator::attempt_allocation_locked(size_t word_size) { - HeapWord* result = mutator_alloc_region()->attempt_allocation_locked(word_size); - assert(result != NULL || mutator_alloc_region()->get() == NULL, - "Must not have a mutator alloc region if there is no memory, but is " PTR_FORMAT, p2i(mutator_alloc_region()->get())); + uint node_index = current_node_index(); + HeapWord* result = mutator_alloc_region(node_index)->attempt_allocation_locked(word_size); + assert(result != NULL || mutator_alloc_region(node_index)->get() == NULL, + "Must not have a mutator alloc region if there is no memory, but is " PTR_FORMAT, p2i(mutator_alloc_region(node_index)->get())); return result; } inline HeapWord* G1Allocator::attempt_allocation_force(size_t word_size) { - return mutator_alloc_region()->attempt_allocation_force(word_size); + uint node_index = current_node_index(); + return mutator_alloc_region(node_index)->attempt_allocation_force(word_size); } inline PLAB* G1PLABAllocator::alloc_buffer(G1HeapRegionAttr dest) { --- old/src/hotspot/share/gc/g1/g1CollectedHeap.cpp 2019-09-30 17:13:03.556029664 -0700 +++ new/src/hotspot/share/gc/g1/g1CollectedHeap.cpp 2019-09-30 17:13:03.184029677 -0700 @@ -169,12 +169,15 @@ // Private methods. -HeapRegion* G1CollectedHeap::new_region(size_t word_size, HeapRegionType type, bool do_expand) { +HeapRegion* G1CollectedHeap::new_region(size_t word_size, + HeapRegionType type, + bool do_expand, + uint node_index) { assert(!is_humongous(word_size) || word_size <= HeapRegion::GrainWords, "the only time we use this to allocate a humongous region is " "when we are allocating a single humongous region"); - HeapRegion* res = _hrm->allocate_free_region(type); + HeapRegion* res = _hrm->allocate_free_region(type, node_index); if (res == NULL && do_expand && _expand_heap_after_alloc_failure) { // Currently, only attempts to allocate GC alloc regions set @@ -186,12 +189,15 @@ log_debug(gc, ergo, heap)("Attempt heap expansion (region allocation request failed). Allocation request: " SIZE_FORMAT "B", word_size * HeapWordSize); - if (expand(word_size * HeapWordSize)) { - // Given that expand() succeeded in expanding the heap, and we + assert(word_size * HeapWordSize < HeapRegion::GrainBytes, + "This kind of expansion should never be more than one region. Size: " SIZE_FORMAT, + word_size * HeapWordSize); + if (expand_single_region(node_index)) { + // Given that expand_single_region() succeeded in expanding the heap, and we // always expand the heap by an amount aligned to the heap // region size, the free list should in theory not be empty. // In either case allocate_free_region() will check for NULL. - res = _hrm->allocate_free_region(type); + res = _hrm->allocate_free_region(type, node_index); } else { _expand_heap_after_alloc_failure = false; } @@ -1022,7 +1028,7 @@ void G1CollectedHeap::prepare_heap_for_full_collection() { // Make sure we'll choose a new allocation region afterwards. - _allocator->release_mutator_alloc_region(); + _allocator->release_mutator_alloc_regions(); _allocator->abandon_gc_alloc_regions(); // We may have added regions to the current incremental collection @@ -1066,7 +1072,7 @@ // Start a new incremental collection set for the next pause start_new_collection_set(); - _allocator->init_mutator_alloc_region(); + _allocator->init_mutator_alloc_regions(); // Post collection state updates. MetaspaceGC::compute_new_size(); @@ -1383,6 +1389,21 @@ return regions_to_expand > 0; } +bool G1CollectedHeap::expand_single_region(uint node_index) { + log_debug(gc, ergo, heap)("Expand the heap by a single region"); + + uint expanded_by = _hrm->expand_on_preferred_node(node_index); + + if (expanded_by == 0) { + assert(is_maximal_no_gc(), "Should be no regions left, available: %u", _hrm->available()); + log_debug(gc, ergo, heap)("Did not expand the heap (heap already fully expanded)"); + return false; + } + + policy()->record_new_heap_size(num_regions()); + return true; +} + void G1CollectedHeap::shrink_helper(size_t shrink_bytes) { size_t aligned_shrink_bytes = ReservedSpace::page_align_size_down(shrink_bytes); @@ -1393,7 +1414,6 @@ uint num_regions_removed = _hrm->shrink_by(num_regions_to_remove); size_t shrunk_bytes = num_regions_removed * HeapRegion::GrainBytes; - log_debug(gc, ergo, heap)("Shrink the heap. requested shrinking amount: " SIZE_FORMAT "B aligned shrinking amount: " SIZE_FORMAT "B attempted shrinking amount: " SIZE_FORMAT "B", shrink_bytes, aligned_shrink_bytes, shrunk_bytes); if (num_regions_removed > 0) { @@ -1495,6 +1515,7 @@ _humongous_set("Humongous Region Set", new HumongousRegionSetChecker()), _bot(NULL), _listener(), + _mem_node_mgr(G1MemoryNodeManager::create()), _hrm(NULL), _allocator(NULL), _verifier(NULL), @@ -1778,6 +1799,8 @@ } _workers->initialize_workers(); + _mem_node_mgr->set_page_size(page_size); + // Create the G1ConcurrentMark data structure and thread. // (Must do this late, so that "max_regions" is defined.) _cm = new G1ConcurrentMark(this, prev_bitmap_storage, next_bitmap_storage); @@ -1825,7 +1848,7 @@ dummy_region->set_top(dummy_region->end()); G1AllocRegion::setup(this, dummy_region); - _allocator->init_mutator_alloc_region(); + _allocator->init_mutator_alloc_regions(); // Do create of the monitoring and management support so that // values in the heap have been properly initialized. @@ -3008,7 +3031,7 @@ // Forget the current allocation region (we might even choose it to be part // of the collection set!). - _allocator->release_mutator_alloc_region(); + _allocator->release_mutator_alloc_regions(); calculate_collection_set(evacuation_info, target_pause_time_ms); @@ -3045,7 +3068,7 @@ allocate_dummy_regions(); - _allocator->init_mutator_alloc_region(); + _allocator->init_mutator_alloc_regions(); expand_heap_after_young_collection(); @@ -4548,13 +4571,15 @@ // Methods for the mutator alloc region HeapRegion* G1CollectedHeap::new_mutator_alloc_region(size_t word_size, - bool force) { + bool force, + uint node_index) { assert_heap_locked_or_at_safepoint(true /* should_be_vm_thread */); bool should_allocate = policy()->should_allocate_mutator_region(); if (force || should_allocate) { HeapRegion* new_alloc_region = new_region(word_size, HeapRegionType::Eden, - false /* do_expand */); + false /* do_expand */, + node_index); if (new_alloc_region != NULL) { set_region_short_lived_locked(new_alloc_region); _hr_printer.alloc(new_alloc_region, !should_allocate); --- old/src/hotspot/share/gc/g1/g1CollectedHeap.hpp 2019-09-30 17:13:04.652029625 -0700 +++ new/src/hotspot/share/gc/g1/g1CollectedHeap.hpp 2019-09-30 17:13:04.308029637 -0700 @@ -40,6 +40,7 @@ #include "gc/g1/g1HeapVerifier.hpp" #include "gc/g1/g1HRPrinter.hpp" #include "gc/g1/g1HeapRegionAttr.hpp" +#include "gc/g1/g1MemoryNodeManager.hpp" #include "gc/g1/g1MonitoringSupport.hpp" #include "gc/g1/g1RedirtyCardsQueue.hpp" #include "gc/g1/g1SurvivorRegions.hpp" @@ -192,6 +193,9 @@ // Callback for region mapping changed events. G1RegionMappingChangedListener _listener; + // Manages single or multi node memory. + G1MemoryNodeManager* _mem_node_mgr; + // The sequence of all heap regions in the heap. HeapRegionManager* _hrm; @@ -388,7 +392,10 @@ // attempt to expand the heap if necessary to satisfy the allocation // request. 'type' takes the type of region to be allocated. (Use constants // Old, Eden, Humongous, Survivor defined in HeapRegionType.) - HeapRegion* new_region(size_t word_size, HeapRegionType type, bool do_expand); + HeapRegion* new_region(size_t word_size, + HeapRegionType type, + bool do_expand, + uint node_index = G1MemoryNodeManager::AnyNodeIndex); // Initialize a contiguous set of free regions of length num_regions // and starting at index first so that they appear as a single @@ -463,7 +470,7 @@ // These methods are the "callbacks" from the G1AllocRegion class. // For mutator alloc regions. - HeapRegion* new_mutator_alloc_region(size_t word_size, bool force); + HeapRegion* new_mutator_alloc_region(size_t word_size, bool force, uint node_index); void retire_mutator_alloc_region(HeapRegion* alloc_region, size_t allocated_bytes); @@ -548,11 +555,14 @@ void resize_heap_if_necessary(); + G1MemoryNodeManager* mem_node_mgr() const { return _mem_node_mgr; } + // Expand the garbage-first heap by at least the given size (in bytes!). // Returns true if the heap was expanded by the requested amount; // false otherwise. // (Rounds up to a HeapRegion boundary.) bool expand(size_t expand_bytes, WorkGang* pretouch_workers = NULL, double* expand_time_ms = NULL); + bool expand_single_region(uint node_index); // Returns the PLAB statistics for a given destination. inline G1EvacStats* alloc_buffer_stats(G1HeapRegionAttr dest); @@ -928,7 +938,6 @@ G1CMSubjectToDiscoveryClosure _is_subject_to_discovery_cm; public: - RefToScanQueue *task_queue(uint i) const; uint num_task_queues() const; --- old/src/hotspot/share/gc/g1/g1PageBasedVirtualSpace.cpp 2019-09-30 17:13:05.644029591 -0700 +++ new/src/hotspot/share/gc/g1/g1PageBasedVirtualSpace.cpp 2019-09-30 17:13:05.308029603 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -23,6 +23,7 @@ */ #include "precompiled.hpp" +#include "gc/g1/g1NUMA.inline.hpp" #include "gc/g1/g1PageBasedVirtualSpace.hpp" #include "gc/shared/workgroup.hpp" #include "oops/markWord.hpp" @@ -33,13 +34,13 @@ #include "utilities/align.hpp" #include "utilities/bitMap.inline.hpp" -G1PageBasedVirtualSpace::G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size) : +G1PageBasedVirtualSpace::G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size, MemoryType type) : _low_boundary(NULL), _high_boundary(NULL), _tail_size(0), _page_size(0), - _committed(mtGC), _dirty(mtGC), _special(false), _executable(false) { - initialize_with_page_size(rs, used_size, page_size); + _committed(mtGC), _dirty(mtGC), _special(false), _executable(false), _numa(NULL) { + initialize_with_page_size(rs, used_size, page_size, type); } -void G1PageBasedVirtualSpace::initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size) { +void G1PageBasedVirtualSpace::initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size, MemoryType type) { guarantee(rs.is_reserved(), "Given reserved space must have been reserved already."); vmassert(_low_boundary == NULL, "VirtualSpace already initialized"); @@ -70,6 +71,13 @@ } _tail_size = used_size % _page_size; + + // Set _numa only if: + // 1) This space is for java heap. + // 2) There are multiple memory nodes because some OSes allow enabling UseNUMA. + if (type == mtJavaHeap && G1MemoryNodeManager::mgr()->num_active_nodes() > 1) { + _numa = G1NUMA::numa(); + } } G1PageBasedVirtualSpace::~G1PageBasedVirtualSpace() { @@ -81,6 +89,7 @@ _executable = false; _page_size = 0; _tail_size = 0; + _numa = NULL; } size_t G1PageBasedVirtualSpace::committed_size() const { @@ -207,6 +216,12 @@ } _committed.set_range(start_page, end_page); + if (_numa != NULL) { + char* start_addr = page_start(start_page); + size_t size_in_bytes = size_in_pages * _page_size; + _numa->request_memory_on_node((address)start_addr, size_in_bytes); + } + return zero_filled; } --- old/src/hotspot/share/gc/g1/g1PageBasedVirtualSpace.hpp 2019-09-30 17:13:06.668029555 -0700 +++ new/src/hotspot/share/gc/g1/g1PageBasedVirtualSpace.hpp 2019-09-30 17:13:06.324029567 -0700 @@ -30,6 +30,7 @@ #include "utilities/align.hpp" #include "utilities/bitMap.hpp" +class G1NUMA; class WorkGang; // Virtual space management helper for a virtual space with an OS page allocation @@ -74,6 +75,8 @@ // Indicates whether the committed space should be executable. bool _executable; + G1NUMA* _numa; + // Helper function for committing memory. Commit the given memory range by using // _page_size pages as much as possible and the remainder with small sized pages. void commit_internal(size_t start_page, size_t end_page); @@ -109,7 +112,7 @@ // Returns true if the entire area is not backed by committed memory. bool is_area_uncommitted(size_t start_page, size_t size_in_pages) const; - void initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size); + void initialize_with_page_size(ReservedSpace rs, size_t used_size, size_t page_size, MemoryType type); public: // Commit the given area of pages starting at start being size_in_pages large. @@ -124,7 +127,7 @@ // Initialize the given reserved space with the given base address and the size // actually used. // Prefer to commit in page_size chunks. - G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size); + G1PageBasedVirtualSpace(ReservedSpace rs, size_t used_size, size_t page_size, MemoryType type); // Destruction ~G1PageBasedVirtualSpace(); --- old/src/hotspot/share/gc/g1/g1RegionToSpaceMapper.cpp 2019-09-30 17:13:07.572029524 -0700 +++ new/src/hotspot/share/gc/g1/g1RegionToSpaceMapper.cpp 2019-09-30 17:13:07.232029536 -0700 @@ -24,6 +24,7 @@ #include "precompiled.hpp" #include "gc/g1/g1BiasedArray.hpp" +#include "gc/g1/g1MemoryNodeManager.hpp" #include "gc/g1/g1RegionToSpaceMapper.hpp" #include "logging/log.hpp" #include "memory/allocation.inline.hpp" @@ -42,7 +43,7 @@ size_t commit_factor, MemoryType type) : _listener(NULL), - _storage(rs, used_size, page_size), + _storage(rs, used_size, page_size, type), _region_granularity(region_granularity), _commit_map(rs.size() * commit_factor / region_granularity, mtGC) { guarantee(is_power_of_2(page_size), "must be"); @@ -72,10 +73,11 @@ } virtual void commit_regions(uint start_idx, size_t num_regions, WorkGang* pretouch_gang) { - size_t const start_page = (size_t)start_idx * _pages_per_region; - bool zero_filled = _storage.commit(start_page, num_regions * _pages_per_region); + const size_t start_page = (size_t)start_idx * _pages_per_region; + const size_t size_in_pages = num_regions * _pages_per_region; + bool zero_filled = _storage.commit(start_page, size_in_pages); if (AlwaysPreTouch) { - _storage.pretouch(start_page, num_regions * _pages_per_region, pretouch_gang); + _storage.pretouch(start_page, size_in_pages, pretouch_gang); } _commit_map.set_range(start_idx, start_idx + num_regions); fire_on_commit(start_idx, num_regions, zero_filled); --- old/src/hotspot/share/gc/g1/g1RegionToSpaceMapper.hpp 2019-09-30 17:13:08.588029488 -0700 +++ new/src/hotspot/share/gc/g1/g1RegionToSpaceMapper.hpp 2019-09-30 17:13:08.208029502 -0700 @@ -25,6 +25,7 @@ #ifndef SHARE_GC_G1_G1REGIONTOSPACEMAPPER_HPP #define SHARE_GC_G1_G1REGIONTOSPACEMAPPER_HPP +#include "gc/g1/g1MemoryNodeManager.hpp" #include "gc/g1/g1PageBasedVirtualSpace.hpp" #include "memory/allocation.hpp" #include "utilities/debug.hpp" --- old/src/hotspot/share/gc/g1/g1_globals.hpp 2019-09-30 17:13:09.660029451 -0700 +++ new/src/hotspot/share/gc/g1/g1_globals.hpp 2019-09-30 17:13:09.296029464 -0700 @@ -323,7 +323,6 @@ "reduce these calls, we keep a buffer of extra regions to " \ "absorb small changes in young gen length. This flag takes " \ "the buffer size as an percentage of young gen length") \ - range(0, 100) \ - + range(0, 100) #endif // SHARE_GC_G1_G1_GLOBALS_HPP --- old/src/hotspot/share/gc/g1/heapRegion.cpp 2019-09-30 17:13:10.740029413 -0700 +++ new/src/hotspot/share/gc/g1/heapRegion.cpp 2019-09-30 17:13:10.380029426 -0700 @@ -28,6 +28,7 @@ #include "gc/g1/g1CollectedHeap.inline.hpp" #include "gc/g1/g1CollectionSet.hpp" #include "gc/g1/g1HeapRegionTraceType.hpp" +#include "gc/g1/g1MemoryNodeManager.hpp" #include "gc/g1/g1OopClosures.inline.hpp" #include "gc/g1/heapRegion.inline.hpp" #include "gc/g1/heapRegionBounds.inline.hpp" @@ -248,7 +249,8 @@ _index_in_opt_cset(InvalidCSetIndex), _young_index_in_cset(-1), _surv_rate_group(NULL), _age_index(-1), _prev_top_at_mark_start(NULL), _next_top_at_mark_start(NULL), - _recorded_rs_length(0), _predicted_elapsed_time_ms(0) + _recorded_rs_length(0), _predicted_elapsed_time_ms(0), + _node_index(G1MemoryNodeManager::InvalidNodeIndex) { _rem_set = new HeapRegionRemSet(bot, this); --- old/src/hotspot/share/gc/g1/heapRegion.hpp 2019-09-30 17:13:11.812029376 -0700 +++ new/src/hotspot/share/gc/g1/heapRegion.hpp 2019-09-30 17:13:11.432029389 -0700 @@ -291,6 +291,8 @@ // for the collection set. double _predicted_elapsed_time_ms; + uint _node_index; + // Iterate over the references covered by the given MemRegion in a humongous // object and apply the given closure to them. // Humongous objects are allocated directly in the old-gen. So we need special @@ -685,6 +687,9 @@ // the strong code roots list for this region void strong_code_roots_do(CodeBlobClosure* blk) const; + uint node_index() const { return _node_index; } + void set_node_index(uint node_index) { _node_index = node_index; } + // Verify that the entries on the strong code root list for this // region are live and include at least one pointer into this region. void verify_strong_code_roots(VerifyOption vo, bool* failures) const; --- old/src/hotspot/share/gc/g1/heapRegionManager.cpp 2019-09-30 17:13:12.908029338 -0700 +++ new/src/hotspot/share/gc/g1/heapRegionManager.cpp 2019-09-30 17:13:12.536029351 -0700 @@ -30,6 +30,7 @@ #include "gc/g1/heapRegionManager.inline.hpp" #include "gc/g1/heapRegionSet.inline.hpp" #include "gc/g1/heterogeneousHeapRegionManager.hpp" +#include "logging/logStream.hpp" #include "memory/allocation.hpp" #include "utilities/bitMap.inline.hpp" @@ -103,6 +104,51 @@ return _available_map.at(region); } +// If log is enabled, compare actual node index and the node index. If those are different +// return the actual node index. +// If log is disabled, just return the node index. +static uint verify_actual_node_index(HeapWord* addr, uint node_index) { + LogTarget(Debug, gc, heap, numa, verification) lt; + + if (lt.is_enabled()) { + LogStream ls(lt); + + uint actual_node_index = G1MemoryNodeManager::mgr()->index_of_address(addr); + if (node_index != actual_node_index) { + ls.print_cr("Heap Region (%u) has different node index. actual index=%u, index=%u", + G1CollectedHeap::heap()->addr_to_region(addr), actual_node_index, node_index); + } + return actual_node_index; + } + return node_index; +} + +HeapRegion* HeapRegionManager::allocate_free_region(HeapRegionType type, uint requested_node_index) { + G1MemoryNodeManager* mgr = G1MemoryNodeManager::mgr(); + HeapRegion* hr = NULL; + bool from_head = !type.is_young(); + + if (mgr->num_active_nodes() > 1 && mgr->is_valid_node_index(requested_node_index)) { + // Try to allocate with requested node index. + hr = _free_list.remove_region_with_node_index(from_head, requested_node_index, NULL); + } + + if (hr == NULL) { + // If there's a single active node or we did not get a region from our requested node, + // try without requested node index. + hr = _free_list.remove_region(from_head); + } + + if (hr != NULL) { + assert(hr->next() == NULL, "Single region should not have next"); + assert(is_available(hr->hrm_index()), "Must be committed"); + + verify_actual_node_index(hr->bottom(), hr->node_index()); + } + + return hr; +} + #ifdef ASSERT bool HeapRegionManager::is_free(HeapRegion* hr) const { return _free_list.contains(hr); @@ -139,6 +185,11 @@ guarantee(num_regions >= 1, "Need to specify at least one region to uncommit, tried to uncommit zero regions at %u", start); guarantee(_num_committed >= num_regions, "pre-condition"); + // Reset node index to distinguish with committed regions. + for (uint i = start; i < start + num_regions; i++) { + at(i)->set_node_index(G1MemoryNodeManager::InvalidNodeIndex); + } + // Print before uncommitting. if (G1CollectedHeap::heap()->hr_printer()->is_active()) { for (uint i = start; i < start + num_regions; i++) { @@ -162,6 +213,34 @@ _card_counts_mapper->uncommit_regions(start, num_regions); } +static void print_node_id_of_regions(uint start, uint num_regions) { + LogTarget(Trace, gc, heap, numa) lt; + + if (lt.is_enabled()) { + LogStream ls(lt); + + // Below logs are checked by TestG1NUMATouchRegions.java. + ls.print_cr("Numa id of heap regions from %u to %u", start, start + num_regions - 1); + ls.print_cr("Heap Region# : numa id of pages"); + + for (uint i = start; i < start + num_regions; i++) { + ls.print_cr("%6u : %02u", i, G1CollectedHeap::heap()->region_at(i)->node_index()); + } + } +} + +// Set node index of the given HeapRegion. +// If AlwaysPreTouch is enabled, set with actual node index. +// If it is disabled, set with preferred node index which is already decided. +static void set_heapregion_node_index(HeapRegion* hr) { + uint node_index = G1MemoryNodeManager::mgr()->preferred_index_for_address(hr->bottom()); + if(AlwaysPreTouch) { + // If we already pretouched, we can check actual node index here. + node_index = verify_actual_node_index(hr->bottom(), node_index); + } + hr->set_node_index(node_index); +} + void HeapRegionManager::make_regions_available(uint start, uint num_regions, WorkGang* pretouch_gang) { guarantee(num_regions > 0, "No point in calling this for zero regions"); commit_regions(start, num_regions, pretouch_gang); @@ -186,8 +265,13 @@ MemRegion mr(bottom, bottom + HeapRegion::GrainWords); hr->initialize(mr); + // Set node index of the heap region after initialization but before inserting + // to free list. + set_heapregion_node_index(hr); insert_into_free_list(at(i)); } + + print_node_id_of_regions(start, num_regions); } MemoryUsage HeapRegionManager::get_auxiliary_data_memory_usage() const { @@ -235,6 +319,37 @@ return expanded; } + uint HeapRegionManager::expand_on_preferred_node(uint preferred_index) { + uint expand_candidate = UINT_MAX; + for (uint i = 0; i < max_length(); i++) { + if (is_available(i)) { + // Already in use continue + continue; + } + // Always save the candidate so we can expand later on. + expand_candidate = i; + if (is_on_preferred_index(expand_candidate, preferred_index)) { + // We have found a candidate on the preffered node, break. + break; + } + } + + if (expand_candidate == UINT_MAX) { + // No regions left, expand failed. + return 0; + } + + make_regions_available(expand_candidate, 1, NULL); + return 1; + } + + bool HeapRegionManager::is_on_preferred_index(uint region_index, uint preferred_node_index) { + uint region_node_index = G1MemoryNodeManager::mgr()->preferred_index_for_address( + G1CollectedHeap::heap()->bottom_addr_for_region(region_index)); + return region_node_index == preferred_node_index || + preferred_node_index == G1MemoryNodeManager::AnyNodeIndex; + } + uint HeapRegionManager::find_contiguous(size_t num, bool empty_only) { uint found = 0; size_t length_found = 0; --- old/src/hotspot/share/gc/g1/heapRegionManager.hpp 2019-09-30 17:13:14.016029299 -0700 +++ new/src/hotspot/share/gc/g1/heapRegionManager.hpp 2019-09-30 17:13:13.652029312 -0700 @@ -108,6 +108,9 @@ // sequence could be found, otherwise res_idx contains the start index of this range. uint find_empty_from_idx_reverse(uint start_idx, uint* res_idx) const; + // Checks the G1MemoryNodeManager to see if this region is on the preferred node. + bool is_on_preferred_index(uint region_index, uint preferred_node_index); + protected: G1HeapRegionTable _regions; G1RegionToSpaceMapper* _heap_mapper; @@ -174,15 +177,8 @@ _free_list.add_ordered(list); } - virtual HeapRegion* allocate_free_region(HeapRegionType type) { - HeapRegion* hr = _free_list.remove_region(!type.is_young()); - - if (hr != NULL) { - assert(hr->next() == NULL, "Single region should not have next"); - assert(is_available(hr->hrm_index()), "Must be committed"); - } - return hr; - } + // Allocate a free region with specific node index. If fails allocate with next node index. + virtual HeapRegion* allocate_free_region(HeapRegionType type, uint requested_node_index); inline void allocate_free_regions_starting_at(uint first, uint num_regions); @@ -227,6 +223,9 @@ // this. virtual uint expand_at(uint start, uint num_regions, WorkGang* pretouch_workers); + // Try to expand on the given node index. + virtual uint expand_on_preferred_node(uint node_index); + // Find a contiguous set of empty regions of length num. Returns the start index of // that set, or G1_NO_HRM_INDEX. virtual uint find_contiguous_only_empty(size_t num) { return find_contiguous(num, true); } --- old/src/hotspot/share/gc/g1/heapRegionSet.hpp 2019-09-30 17:13:15.120029261 -0700 +++ new/src/hotspot/share/gc/g1/heapRegionSet.hpp 2019-09-30 17:13:14.740029274 -0700 @@ -181,6 +181,10 @@ // Removes from head or tail based on the given argument. HeapRegion* remove_region(bool from_head); + HeapRegion* remove_region_with_node_index(bool from_head, + const uint requested_node_index, + uint* region_node_index); + // Merge two ordered lists. The result is also ordered. The order is // determined by hrm_index. void add_ordered(FreeRegionList* from_list); --- old/src/hotspot/share/gc/g1/heapRegionSet.inline.hpp 2019-09-30 17:13:16.192029223 -0700 +++ new/src/hotspot/share/gc/g1/heapRegionSet.inline.hpp 2019-09-30 17:13:15.828029236 -0700 @@ -25,6 +25,7 @@ #ifndef SHARE_GC_G1_HEAPREGIONSET_INLINE_HPP #define SHARE_GC_G1_HEAPREGIONSET_INLINE_HPP +#include "gc/g1/g1NUMA.inline.hpp" #include "gc/g1/heapRegionSet.hpp" inline void HeapRegionSetBase::add(HeapRegion* hr) { @@ -147,4 +148,72 @@ return hr; } +inline HeapRegion* FreeRegionList::remove_region_with_node_index(bool from_head, + const uint requested_node_index, + uint* allocated_node_index) { + assert(UseNUMA, "Invariant"); + + HeapRegion* cur; + G1NUMA* numa = G1NUMA::numa(); + + if (!numa->is_valid_numa_index(requested_node_index)) { + return NULL; + } + + // Multiple of 3 is just random number to limit iterations. + uint const max_search_depth = 3 * numa->num_active_numa_ids(); + + // Find the region to use, searching from _head or _tail as requested. + size_t cur_depth = 0; + if (from_head) { + for (cur = _head; + cur != NULL && cur_depth < max_search_depth; + cur = cur->next(), ++cur_depth) { + if (requested_node_index == cur->node_index()) { + break; + } + } + } else { + for (cur = _tail; + cur != NULL && cur_depth < max_search_depth; + cur = cur->prev(), ++cur_depth) { + if (requested_node_index == cur->node_index()) { + break; + } + } + } + + // Didn't find a region to use. + if (cur == NULL || cur_depth >= max_search_depth) { + return NULL; + } + + // Splice the region out of the list. + HeapRegion* prev = cur->prev(); + HeapRegion* next = cur->next(); + if (prev == NULL) { + _head = next; + } else { + prev->set_next(next); + } + if (next == NULL) { + _tail = prev; + } else { + next->set_prev(prev); + } + cur->set_prev(NULL); + cur->set_next(NULL); + + if (_last == cur) { + _last = NULL; + } + + remove(cur); + if (allocated_node_index != NULL) { + *allocated_node_index = cur->node_index(); + } + + return cur; +} + #endif // SHARE_GC_G1_HEAPREGIONSET_INLINE_HPP --- old/src/hotspot/share/gc/g1/heterogeneousHeapRegionManager.cpp 2019-09-30 17:13:17.312029184 -0700 +++ new/src/hotspot/share/gc/g1/heterogeneousHeapRegionManager.cpp 2019-09-30 17:13:16.936029198 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -263,7 +263,7 @@ return num_regions_found; } -HeapRegion* HeterogeneousHeapRegionManager::allocate_free_region(HeapRegionType type) { +HeapRegion* HeterogeneousHeapRegionManager::allocate_free_region(HeapRegionType type, uint node_index) { // We want to prevent mutators from proceeding when we have borrowed regions from the last collection. This // will force a full collection to remedy the situation. --- old/src/hotspot/share/gc/g1/heterogeneousHeapRegionManager.hpp 2019-09-30 17:13:18.372029148 -0700 +++ new/src/hotspot/share/gc/g1/heterogeneousHeapRegionManager.hpp 2019-09-30 17:13:18.000029160 -0700 @@ -119,7 +119,7 @@ void prepare_for_full_collection_start(); void prepare_for_full_collection_end(); - virtual HeapRegion* allocate_free_region(HeapRegionType type); + virtual HeapRegion* allocate_free_region(HeapRegionType type, uint node_index); // Return maximum number of regions that heap can expand to. uint max_expandable_length() const; --- old/src/hotspot/share/logging/logPrefix.hpp 2019-09-30 17:13:21.572029036 -0700 +++ new/src/hotspot/share/logging/logPrefix.hpp 2019-09-30 17:13:21.200029049 -0700 @@ -57,6 +57,8 @@ LOG_PREFIX(GCId::print_prefix, LOG_TAGS(gc, ergo, ihop)) \ LOG_PREFIX(GCId::print_prefix, LOG_TAGS(gc, ergo, refine)) \ LOG_PREFIX(GCId::print_prefix, LOG_TAGS(gc, heap)) \ + LOG_PREFIX(GCId::print_prefix, LOG_TAGS(gc, heap, numa)) \ + LOG_PREFIX(GCId::print_prefix, LOG_TAGS(gc, heap, numa, verification)) \ LOG_PREFIX(GCId::print_prefix, LOG_TAGS(gc, heap, region)) \ LOG_PREFIX(GCId::print_prefix, LOG_TAGS(gc, freelist)) \ LOG_PREFIX(GCId::print_prefix, LOG_TAGS(gc, humongous)) \ --- old/src/hotspot/share/logging/logTag.hpp 2019-09-30 17:13:22.604029000 -0700 +++ new/src/hotspot/share/logging/logTag.hpp 2019-09-30 17:13:22.244029013 -0700 @@ -108,6 +108,7 @@ LOG_TAG(nestmates) \ LOG_TAG(nmethod) \ LOG_TAG(normalize) \ + LOG_TAG(numa) \ LOG_TAG(objecttagging) \ LOG_TAG(obsolete) \ LOG_TAG(oldobject) \ --- old/src/hotspot/share/prims/whitebox.cpp 2019-09-30 17:13:23.660028963 -0700 +++ new/src/hotspot/share/prims/whitebox.cpp 2019-09-30 17:13:23.284028976 -0700 @@ -619,6 +619,29 @@ THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1AuxiliaryMemoryUsage: G1 GC is not enabled"); WB_END +WB_ENTRY(jint, WB_G1ActiveMemoryNodeCount(JNIEnv* env, jobject o)) + if (UseG1GC) { + G1MemoryNodeManager* mgr = G1MemoryNodeManager::mgr(); + return (jint)mgr->num_active_nodes(); + } + THROW_MSG_0(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1ActiveMemoryNodeCount: G1 GC is not enabled"); +WB_END + +WB_ENTRY(jintArray, WB_G1MemoryNodeIds(JNIEnv* env, jobject o)) + if (UseG1GC) { + G1MemoryNodeManager* mgr = G1MemoryNodeManager::mgr(); + int num_node_ids = (int)mgr->num_active_nodes(); + const int* node_ids = mgr->node_ids(); + + typeArrayOop result = oopFactory::new_intArray(num_node_ids, CHECK_NULL); + for (int i = 0; i < num_node_ids; i++) { + result->int_at_put(i, (jint)node_ids[i]); + } + return (jintArray) JNIHandles::make_local(env, result); + } + THROW_MSG_NULL(vmSymbols::java_lang_UnsupportedOperationException(), "WB_G1MemoryNodeIds: G1 GC is not enabled"); +WB_END + class OldRegionsLivenessClosure: public HeapRegionClosure { private: @@ -2199,6 +2222,8 @@ {CC"g1StartConcMarkCycle", CC"()Z", (void*)&WB_G1StartMarkCycle }, {CC"g1AuxiliaryMemoryUsage", CC"()Ljava/lang/management/MemoryUsage;", (void*)&WB_G1AuxiliaryMemoryUsage }, + {CC"g1ActiveMemoryNodeCount", CC"()I", (void*)&WB_G1ActiveMemoryNodeCount }, + {CC"g1MemoryNodeIds", CC"()[I", (void*)&WB_G1MemoryNodeIds }, {CC"g1GetMixedGCInfo", CC"(I)[J", (void*)&WB_G1GetMixedGCInfo }, #endif // INCLUDE_G1GC #if INCLUDE_G1GC || INCLUDE_PARALLELGC --- old/src/hotspot/share/runtime/arguments.cpp 2019-09-30 17:13:24.708028927 -0700 +++ new/src/hotspot/share/runtime/arguments.cpp 2019-09-30 17:13:24.340028940 -0700 @@ -4105,10 +4105,10 @@ } // UseNUMAInterleaving is set to ON for all collectors and // platforms when UseNUMA is set to ON. NUMA-aware collectors - // such as the parallel collector for Linux and Solaris will + // such as Parallel GC for Linux and Solaris or G1 GC for Linux will // interleave old gen and survivor spaces on top of NUMA // allocation policy for the eden space. - // Non NUMA-aware collectors such as CMS, G1 and Serial-GC on + // Non NUMA-aware collectors such as CMS and Serial-GC on // all platforms and ParallelGC on Windows will interleave all // of the heap spaces across NUMA nodes. if (FLAG_IS_DEFAULT(UseNUMAInterleaving)) { --- old/src/hotspot/share/runtime/os.hpp 2019-09-30 17:13:25.852028887 -0700 +++ new/src/hotspot/share/runtime/os.hpp 2019-09-30 17:13:25.480028900 -0700 @@ -390,6 +390,13 @@ static bool numa_topology_changed(); static int numa_get_group_id(); + enum NumaIdState { + InvalidId = -1, + AnyId = -2 + }; + + static int numa_get_address_id(uintptr_t address); + // Page manipulation struct page_info { size_t size; --- old/test/lib/sun/hotspot/WhiteBox.java 2019-09-30 17:13:28.024028811 -0700 +++ new/test/lib/sun/hotspot/WhiteBox.java 2019-09-30 17:13:27.652028824 -0700 @@ -193,6 +193,9 @@ return parseCommandLine0(commandline, delim, args); } + public native int g1ActiveMemoryNodeCount(); + public native int[] g1MemoryNodeIds(); + // Parallel GC public native long psVirtualSpaceAlignment(); public native long psHeapGenerationAlignment(); --- /dev/null 2019-09-30 10:03:02.736928411 -0700 +++ new/src/hotspot/share/gc/g1/g1MemoryNodeManager.cpp 2019-09-30 17:13:28.744028786 -0700 @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#include "precompiled.hpp" +#include "gc/g1/heapRegionType.hpp" +#include "gc/g1/g1MemoryNodeManager.hpp" +#include "gc/g1/g1NUMA.inline.hpp" + +G1MemoryNodeManager* G1MemoryNodeManager::_inst = NULL; + +const int* G1MemoryNodeManager::node_ids() const { + static const int dummy_id = 0; + return &dummy_id; +} + +class G1MemoryMultiNodeManager : public G1MemoryNodeManager { + G1NUMA* _numa; + +public: + // The given numa instance will be deleted by the destructor. + G1MemoryMultiNodeManager(G1NUMA* numa); + ~G1MemoryMultiNodeManager(); + + virtual void set_page_size(size_t page_size); + + virtual uint num_active_nodes() const; + + virtual const int* node_ids() const; + + virtual uint index_of_current_thread() const; + + virtual bool is_valid_node_index(uint node_index) const; + + virtual uint preferred_index_for_address(HeapWord* addr) const; + + virtual uint index_of_address(HeapWord* addr) const; +}; + +G1MemoryNodeManager* G1MemoryNodeManager::create() { + guarantee(_inst == NULL, "Should be called once."); + + // Use multi node manager: + // 1. On Linux + // 2. If UseNUMA is enabled + // 3. If there are more than 1 active numa nodes. + LINUX_ONLY(if (UseNUMA) { + // Create G1NUMA to check current active numa nodes. + G1NUMA* numa = new G1NUMA(); + + numa->initialize(); + + if (numa->num_active_numa_ids() > 1) { + G1NUMA::set_numa(numa); + _inst = new G1MemoryMultiNodeManager(numa); + return _inst; + } + + delete numa; + }) + + _inst = new G1MemoryNodeManager(); + + return _inst; +} + +G1MemoryMultiNodeManager::G1MemoryMultiNodeManager(G1NUMA* numa) : _numa(numa) { } + +G1MemoryMultiNodeManager::~G1MemoryMultiNodeManager() { + delete _numa; +} + +void G1MemoryMultiNodeManager::set_page_size(size_t page_size) { + _numa->set_page_size(page_size); +} + +uint G1MemoryMultiNodeManager::num_active_nodes() const { + return _numa->num_active_numa_ids(); +} + +const int* G1MemoryMultiNodeManager::node_ids() const { + return _numa->numa_ids(); +} + +uint G1MemoryMultiNodeManager::index_of_current_thread() const { + return _numa->index_of_current_thread(); +} + +bool G1MemoryMultiNodeManager::is_valid_node_index(uint node_index) const { + return _numa->is_valid_numa_index(node_index); +} + +uint G1MemoryMultiNodeManager::preferred_index_for_address(HeapWord* addr) const { + return _numa->preferred_index_for_address(addr); +} + +uint G1MemoryMultiNodeManager::index_of_address(HeapWord* addr) const { + return _numa->index_of_address(addr); +} --- /dev/null 2019-09-30 10:03:02.736928411 -0700 +++ new/src/hotspot/share/gc/g1/g1MemoryNodeManager.hpp 2019-09-30 17:13:29.812028749 -0700 @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#ifndef SHARE_VM_GC_G1_MEMORY_NODE_MANAGER_HPP +#define SHARE_VM_GC_G1_MEMORY_NODE_MANAGER_HPP + +#include "memory/virtualspace.hpp" +#include "memory/allocation.hpp" +#include "runtime/os.hpp" + +// Helper class to manage memory nodes. +// G1MemoryMultiNodeManager will be created if UseNUMA is enabled and active +// NUMA nodes are more than one. Otherwise, G1MemoryNodeManager will be created. +class G1MemoryNodeManager : public CHeapObj { + static G1MemoryNodeManager* _inst; + +protected: + G1MemoryNodeManager() { } + +public: + static const uint InvalidNodeIndex = UINT_MAX; + static const uint AnyNodeIndex = InvalidNodeIndex - 1; + + static G1MemoryNodeManager* mgr() { return _inst; } + + static G1MemoryNodeManager* create(); + + virtual ~G1MemoryNodeManager() { } + + virtual void set_page_size(size_t page_size) { } + + // Print current active memory node count. + virtual uint num_active_nodes() const { return 1; } + + // Returns memory node ids + virtual const int* node_ids() const; + + virtual uint index_of_current_thread() const { return 0; } + + virtual bool is_valid_node_index(uint node_index) const { + // Single node index should be always 0. + return node_index == 0; + } + + virtual uint preferred_index_for_address(HeapWord* addr) const { return 0; } + + // Retrieve node index of the given address. + virtual uint index_of_address(HeapWord* addr) const { return 0; } +}; + +#endif // SHARE_VM_GC_G1_MEMORY_NODE_MANAGER_HPP --- /dev/null 2019-09-30 10:03:02.736928411 -0700 +++ new/src/hotspot/share/gc/g1/g1NUMA.cpp 2019-09-30 17:13:30.828028714 -0700 @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#include "precompiled.hpp" +#include "gc/g1/g1CollectedHeap.inline.hpp" +#include "gc/g1/g1NUMA.inline.hpp" +#include "runtime/os.hpp" + +G1NUMA* G1NUMA::_inst = NULL; + +void G1NUMA::init_numa_id_to_index_map(const int* numa_ids, uint num_numa_ids) { + int max_numa_id = 0; + for (uint i = 0; i < num_numa_ids; i++) { + if (numa_ids[i] > max_numa_id) { + max_numa_id = numa_ids[i]; + } + } + + _len_numa_id_to_index_map = max_numa_id + 1; + _numa_id_to_index_map = NEW_C_HEAP_ARRAY(uint, _len_numa_id_to_index_map, mtGC); + // Set all indices with invalid numa id. + for (int i = 0; i < _len_numa_id_to_index_map; i++) { + _numa_id_to_index_map[i] = G1MemoryNodeManager::InvalidNodeIndex; + } + + // Set the indices for the actually retrieved numa ids. + for (uint i = 0; i < num_numa_ids; i++) { + int numa_id = numa_ids[i]; + guarantee(is_valid_numa_id(numa_id), "must be representable in map, numa id(%d)", numa_id); + _numa_id_to_index_map[numa_id] = i; + } +} + +// Request the given memory to locate on preferred node. +// There are 2 things to consider. +// First, size comparison for G1HeapRegionSize and page size. +// Second, the memory is assumed to be evenly split. As the preferred id will be +// decided in round-robin manner, we can know the numa id from address or even +// from HeapRegion index. +// +// Examples of 4 numa ids with non-preferred numa id. +// 1. G1HeapRegionSize is larger than or equal to page size. +// * Page size: |-0--||-1--||-2--||-3--||-4--||-5--||-6--||-7--||-8--||-9--||-10-||-11-||-12-||-13-||-14-||-15-| +// * G1HeapRegionSize: |----#0----||----#1----||----#2----||----#3----||----#4----||----#5----||----#6----||----#7----| +// 2. G1HeapRegionSize is smaller than page size. +// Memory will be touched one page at a time because G1RegionToSpaceMapper commits +// pages one by one. +// * Page size: |-----0----||-----1----||-----2----||-----3----||-----4----||-----5----||-----6----||-----7----| +// * G1HeapRegionSize: |-#0-||-#1-||-#2-||-#3-||-#4-||-#5-||-#6-||-#7-||-#8-||-#9-||#10-||#11-||#12-||#13-||#14-||#15-| +void G1NUMA::request_memory_on_node(address aligned_address, size_t size_in_bytes) { + assert(is_aligned(aligned_address, _page_size), "Given address (" PTR_FORMAT ") should be aligned.", p2i(aligned_address)); + assert(is_aligned(size_in_bytes, _page_size), "Given size (" SIZE_FORMAT ") should be aligned.", size_in_bytes); + + if (size_in_bytes == 0) { + return; + } + + // If we don't have preferred numa id, touch the given area with round-robin manner. + size_t chunk_size; + if (HeapRegion::GrainBytes >= _page_size) { + chunk_size = HeapRegion::GrainBytes; + } else { + chunk_size = _page_size; + } + + assert(is_aligned(size_in_bytes, chunk_size), "Size to touch " SIZE_FORMAT " should be aligned to " SIZE_FORMAT, + size_in_bytes, chunk_size); + + address start_addr = aligned_address; + address end_addr = aligned_address + size_in_bytes; + + log_debug(gc, heap, numa)("Request memory [" PTR_FORMAT ", " PTR_FORMAT + "), chunk_size=" SIZE_FORMAT "KB with round-robin manner.", + p2i(start_addr), p2i(end_addr), chunk_size / K); + + do { + // Numa id of each HeapRegion is decided. + uint numa_index = preferred_index_for_address((HeapWord*)start_addr); + + log_trace(gc, heap, numa)("Request memory [" PTR_FORMAT ", " PTR_FORMAT ") to be numa id (%d).", + p2i(start_addr), p2i(start_addr + chunk_size), _numa_ids[numa_index]); + os::numa_make_local((char*)start_addr, chunk_size, _numa_ids[numa_index]); + + start_addr += chunk_size; + } while (start_addr < end_addr); +} + +bool G1NUMA::initialize() { + assert(UseNUMA, "Invariant"); + + size_t num_numa_ids = os::numa_get_groups_num(); + + _numa_ids = NEW_C_HEAP_ARRAY(int, num_numa_ids, mtGC); + _num_active_numa_ids = (uint)os::numa_get_leaf_groups(_numa_ids, num_numa_ids); + + init_numa_id_to_index_map(_numa_ids, _num_active_numa_ids); + + return true; +} + +uint G1NUMA::index_of_current_thread() const { + int numa_id = os::numa_get_group_id(); + return index_of_numa_id(numa_id); +} + +void G1NUMA::set_page_size(size_t page_size) { + _page_size = page_size; +} + +G1NUMA::~G1NUMA() { + FREE_C_HEAP_ARRAY(int, _numa_id_to_index_map); + FREE_C_HEAP_ARRAY(int, _numa_ids); + _inst = NULL; +} + +uint G1NUMA::preferred_index_for_address(HeapWord* address) const { + uint region_index = G1CollectedHeap::heap()->addr_to_region(address); + if (HeapRegion::GrainBytes >= _page_size) { + // Simple case, pages are smaller than the region so we + // can just alternate over the nodes. + return region_index % _num_active_numa_ids; + } else { + // Multiple regions in one page, so we need to make sure the + // regions within a page is preferred on the same node. + size_t regions_per_page = _page_size / HeapRegion::GrainBytes; + return (region_index / regions_per_page) % _num_active_numa_ids; + } +} + +uint G1NUMA::index_of_address(HeapWord* address) const { + int numa_id = os::numa_get_address_id((uintptr_t)address); + return index_of_numa_id(numa_id); +} --- /dev/null 2019-09-30 10:03:02.736928411 -0700 +++ new/src/hotspot/share/gc/g1/g1NUMA.hpp 2019-09-30 17:13:31.936028675 -0700 @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#ifndef SHARE_VM_GC_G1_G1NUMA_HPP +#define SHARE_VM_GC_G1_G1NUMA_HPP + +#include "runtime/globals.hpp" + +// Manages NUMA node related information. +// Provides a conversion between NUMA indices (sequential, starting +// from zero) and NUMA ids (which may not start at zero and may not be +// sequential). Indices are generally used by clients, and mapped to +// ids when dealing with the OS/hardware layer. +class G1NUMA : public CHeapObj { + // Mapping of available numa ids to some 0-based index which can be used for + // fast resource management. I.e. for every numa id provides a unique value in + // the range from [0, {# of numa nodes-1}]. + // For invalid numa id, return G1MemoryNodeManager::InvalidNodeIndex. + // So the caller need exception handling. + uint* _numa_id_to_index_map; + // Length of numa_id to index map. + int _len_numa_id_to_index_map; + + // Necessary when touching memory. + size_t _page_size; + + // Current active numa ids. + int* _numa_ids; + // Total number of numa ids. + uint _num_active_numa_ids; + + static G1NUMA* _inst; + + // Creates numa id and numa index mapping table of _numa_id_to_index_map. + void init_numa_id_to_index_map(const int* numa_ids, uint num_numa_ids); + +public: + G1NUMA() : _numa_id_to_index_map(NULL), _len_numa_id_to_index_map(0), + _page_size(0), _numa_ids(NULL), _num_active_numa_ids(0) { } + ~G1NUMA(); + + static G1NUMA* numa() { return _inst; } + + static void set_numa(G1NUMA* numa) { + guarantee(_inst == NULL, "Should be called once."); + + _inst = numa; + } + + void request_memory_on_node(address aligned_address, size_t size_in_bytes); + + bool initialize(); + + inline bool is_valid_numa_id(int numa_id); + + inline bool is_valid_numa_index(uint numa_index) const; + + inline uint num_active_numa_ids() const; + + // Returns numa index of the given numa id. + // Returns G1MemoryNodeManager::InvalidNodeIndex if the given numa id is invalid. + inline uint index_of_numa_id(int numa_id) const; + + uint index_of_current_thread() const; + + // Returns numa id of the given numa index. + inline int numa_id_of_index(uint numa_index) const; + + // Initialize with information after heap is created. + // If AlwaysPreTouch is disabled _numa_id_of_regions_table is initialized. + void set_page_size(size_t page_size); + + // Returns current active numa ids. + const int* numa_ids() const { return _numa_ids; } + + // Returns the preferred index for the given address. + // This assumes that HeapRegions are evenly spit, so we can decide preferred index + // with the given address. + uint preferred_index_for_address(HeapWord* address) const; + + // Returns numa index of the given address via system call. + uint index_of_address(HeapWord* address) const; +}; + +#endif // SHARE_VM_GC_G1_G1NUMA_HPP --- /dev/null 2019-09-30 10:03:02.736928411 -0700 +++ new/src/hotspot/share/gc/g1/g1NUMA.inline.hpp 2019-09-30 17:13:33.044028636 -0700 @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + * + */ + +#ifndef SHARE_VM_GC_G1_NUMA_INLINE_HPP +#define SHARE_VM_GC_G1_NUMA_INLINE_HPP + +#include "gc/g1/g1MemoryNodeManager.hpp" +#include "gc/g1/g1NUMA.hpp" +#include "runtime/atomic.hpp" + +inline bool G1NUMA::is_valid_numa_id(int numa_id) { + // Valid numa id should be one of active numa ids. + for (uint i = 0; i < _num_active_numa_ids; i++) { + if (_numa_ids[i] == numa_id) { + return true; + } + } + return false; +} + +inline bool G1NUMA::is_valid_numa_index(uint numa_index) const { + // Valid numa index should be less than the number of active numa ids. + return numa_index < _num_active_numa_ids; +} + +inline uint G1NUMA::num_active_numa_ids() const { + assert(_num_active_numa_ids > 0, "just checking"); + return _num_active_numa_ids; +} + +inline uint G1NUMA::index_of_numa_id(int numa_id) const { + // Don't need call is_valid_numa_id, as _numa_id_to_index_map + // may return G1MemoryNodeManager::InvalidNodeIndex. + if (numa_id >= 0 && numa_id < _len_numa_id_to_index_map) { + return _numa_id_to_index_map[numa_id]; + } + return G1MemoryNodeManager::InvalidNodeIndex; +} + +inline int G1NUMA::numa_id_of_index(uint numa_index) const { + if (is_valid_numa_index(numa_index)) { + return _numa_ids[numa_index]; + } + return os::InvalidId; +} + +#endif // SHARE_VM_GC_G1_NUMA_INLINE_HPP --- /dev/null 2019-09-30 10:03:02.736928411 -0700 +++ new/test/hotspot/jtreg/gc/g1/numa/TestG1NUMATouchRegions.java 2019-09-30 17:13:34.128028599 -0700 @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package gc.g1; + +/** + * @test TestG1NUMATouchRegions + * @summary Ensure the bottom of the given heap regions are properly touched with requested NUMA id. + * @key gc + * @requires vm.gc.G1 + * @requires os.family == "linux" + * @library /test/lib + * @modules java.base/jdk.internal.misc + * java.management + * @build sun.hotspot.WhiteBox + * @run driver ClassFileInstaller sun.hotspot.WhiteBox + * @run main/othervm -XX:+UseG1GC -Xbootclasspath/a:. -XX:+UseNUMA -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI gc.g1.TestG1NUMATouchRegions + */ + +import jdk.test.lib.process.OutputAnalyzer; +import jdk.test.lib.process.ProcessTools; +import sun.hotspot.WhiteBox; + +public class TestG1NUMATouchRegions { + enum NUMASupportStatus { + NOT_CHECKED, + SUPPORT, + NOT_SUPPORT + }; + + static int G1HeapRegionSize1MB = 1; + static int G1HeapRegionSize8MB = 8; + + static NUMASupportStatus status = NUMASupportStatus.NOT_CHECKED; + + public static void main(String[] args) throws Exception { + // 1. Page size < G1HeapRegionSize + // Test default page with 1MB heap region size + testMemoryTouch("-XX:-UseLargePages", G1HeapRegionSize1MB); + // 2. Page size > G1HeapRegionSize + // Test large page with 1MB heap region size. + testMemoryTouch("-XX:+UseLargePages", G1HeapRegionSize1MB); + // 3. Page size < G1HeapRegionSize + // Test large page with 8MB heap region size. + testMemoryTouch("-XX:+UseLargePages", G1HeapRegionSize8MB); + } + + // On Linux, always UseNUMA is enabled if there is multiple active numa nodes. + static NUMASupportStatus checkNUMAIsEnabled(OutputAnalyzer output) { + boolean supportNUMA = Boolean.parseBoolean(output.firstMatch("\\bUseNUMA\\b.*?=.*?([a-z]+)", 1)); + System.out.println("supportNUMA=" + supportNUMA); + return supportNUMA ? NUMASupportStatus.SUPPORT : NUMASupportStatus.NOT_SUPPORT; + } + + static long parseSizeString(String size) { + long multiplier = 1; + + if (size.endsWith("B")) { + multiplier = 1; + } else if (size.endsWith("K")) { + multiplier = 1024; + } else if (size.endsWith("M")) { + multiplier = 1024 * 1024; + } else if (size.endsWith("G")) { + multiplier = 1024 * 1024 * 1024; + } else { + throw new IllegalArgumentException("Expected memory string '" + size + "'to end with either of: B, K, M, G"); + } + + long longSize = Long.parseUnsignedLong(size.substring(0, size.length() - 1)); + + return longSize * multiplier; + } + + static long heapPageSize(OutputAnalyzer output) { + String HeapPageSizePattern = "Heap: .*page_size=([^ ]+)"; + String str = output.firstMatch(HeapPageSizePattern, 1); + + if (str == null) { + output.reportDiagnosticSummary(); + throw new RuntimeException("Match from '" + HeapPageSizePattern + "' got 'null'"); + } + + return parseSizeString(str); + } + + // 1. -UseLargePages: default page, page size < G1HeapRegionSize + // +UseLargePages: large page size <= G1HeapRegionSize + // + // Each 'int' represents a numa id of single HeapRegion (bottom page). + // e.g. heap region # : numa id of page + // 0 : 00 + // 1 : 01 + static void checkCase1Pattern(OutputAnalyzer output, int index, long g1HeapRegionSize, long actualPageSize, int[] memoryNodeIds) throws Exception { + StringBuilder sb = new StringBuilder(); + + // Append index which means heap region index. + sb.append(String.format("%6d", index)); + sb.append(" : "); + + // Append page node id. + sb.append(String.format("%02d", memoryNodeIds[index])); + + output.shouldContain(sb.toString()); + } + + // 3. +UseLargePages: large page size > G1HeapRegionSize + // + // As a OS page is consist of multiple heap regions, log also should be + // printed multiple times for same numa id. + // e.g. 1MB heap region, 2MB page size + // heap region # (page #): numa id of page + // 0 (0): 00 + // 1 (0): 00 + // 2 (1): 01 + // 3 (1): 01 + static void checkCase2Pattern(OutputAnalyzer output, int index, long g1HeapRegionSize, long actualPageSize, int[] memoryNodeIds) throws Exception { + StringBuilder sb = new StringBuilder(); + + // Append page range. + int lines_to_print = (int)(actualPageSize / g1HeapRegionSize); + for (int i = 0; i < lines_to_print; i++) { + // Append index which means heap region index. + sb.append(String.format("%6d", index * lines_to_print + i)); + sb.append(" : "); + + // Append page node id. + sb.append(String.format("%02d", memoryNodeIds[index])); + + output.shouldContain(sb.toString()); + sb.setLength(0); + } + + output.shouldContain(sb.toString()); + } + + static void checkNUMALog(OutputAnalyzer output, int regionSizeInMB) throws Exception { + WhiteBox wb = WhiteBox.getWhiteBox(); + long g1HeapRegionSize = regionSizeInMB * 1024 * 1024; + long actualPageSize = heapPageSize(output); + long defaultPageSize = (long)wb.getVMPageSize(); + int memoryNodeCount = wb.g1ActiveMemoryNodeCount(); + int[] memoryNodeIds = wb.g1MemoryNodeIds(); + + System.out.println("node count=" + memoryNodeCount + ", actualPageSize=" + actualPageSize); + // Check for the first one set of active numa nodes. + for (int index = 0; index < memoryNodeCount; index++) { + if (actualPageSize <= defaultPageSize) { + checkCase1Pattern(output, index, g1HeapRegionSize, actualPageSize, memoryNodeIds); + } else { + checkCase2Pattern(output, index, g1HeapRegionSize, actualPageSize, memoryNodeIds); + } + } + } + + static void testMemoryTouch(String largePagesSetting, int regionSizeInMB) throws Exception { + // Skip testing with message. + if (status == NUMASupportStatus.NOT_SUPPORT) { + System.out.println("NUMA is not supported"); + return; + } + + ProcessBuilder pb_enabled = ProcessTools.createJavaProcessBuilder( + "-Xbootclasspath/a:.", + "-Xlog:gc+heap+numa=trace,pagesize", + "-XX:+UseG1GC", + "-Xmx128m", + "-Xms128m", + "-XX:+UnlockDiagnosticVMOptions", + "-XX:+WhiteBoxAPI", + "-XX:+PrintFlagsFinal", + "-XX:+UseNUMA", + "-XX:+AlwaysPreTouch", + largePagesSetting, + "-XX:G1HeapRegionSize=" + regionSizeInMB + "m", + "--version"); + OutputAnalyzer output = new OutputAnalyzer(pb_enabled.start()); + + // Check NUMA availability. + if (status == NUMASupportStatus.NOT_CHECKED) { + status = checkNUMAIsEnabled(output); + } + + if (status == NUMASupportStatus.SUPPORT) { + checkNUMALog(output, regionSizeInMB); + } else { + // Exit with message for the first test. + System.out.println("NUMA is not supported"); + } + } +}