--- /dev/null 2019-10-07 09:27:17.753004199 -0700 +++ new/src/hotspot/share/gc/g1/g1MemoryNodeManager.cpp 2019-10-08 20:01:56.004844842 -0700 @@ -0,0 +1,239 @@ +/* + * 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/g1MemoryNodeManager.hpp" +#include "logging/log.hpp" +#include "runtime/globals.hpp" +#include "runtime/os.hpp" + +G1MemoryNodeManager* G1MemoryNodeManager::_inst = NULL; + +const int* G1MemoryNodeManager::node_ids() const { + static const int dummy_id = 0; + return &dummy_id; +} + +class G1MemoryMultiNodeManager : public G1MemoryNodeManager { + // Mapping of available node ids to 0-based index which can be used for + // fast resource management. I.e. for every node id provides a unique value in + // the range from [0, {# of nodes-1}]. + // For invalid node id, return UnknownNodeIndex. + uint* _node_id_to_index_map; + // Length of _num_active_node_ids_id to index map. + int _len_node_id_to_index_map; + + // HeapRegion size + size_t _region_size; + + // Necessary when touching memory. + size_t _page_size; + + // Current active node ids. + int* _node_ids; + // Total number of node ids. + uint _num_active_node_ids; + + size_t region_size() const { + assert(_region_size > 0, "Heap region size is not yet set"); + return _region_size; + } + + size_t page_size() const { + assert(_page_size > 0, "Page size not is yet set"); + return _page_size; + } + + // Returns node index of the given node id. + // Precondition: node_id is an active node id. + inline uint index_of_node_id(int node_id) const; + + // Creates node id and node index mapping table of _node_id_to_index_map. + void init_node_id_to_index_map(const int* node_ids, uint num_node_ids); + +public: + G1MemoryMultiNodeManager(); + ~G1MemoryMultiNodeManager(); + + virtual void set_region_info(size_t region_size, size_t page_size); + + virtual uint num_active_nodes() const; + + virtual bool has_multi_nodes() const { return num_active_nodes() > 1; } + + virtual const int* node_ids() const; + + virtual uint index_of_current_thread() const; + + virtual uint preferred_node_index_for_index(uint region_index) const; + + virtual uint index_of_address(HeapWord* addr) const; + + virtual void request_memory_on_node(char* aligned_address, size_t size_in_bytes, uint node_index); + + virtual const uint max_search_depth() 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 nodes. +#ifdef LINUX + if (UseNUMA) { + // Create G1MemoryMultiNodeManager to check the number of active nodes. + G1MemoryMultiNodeManager* mgr = new G1MemoryMultiNodeManager(); + if (mgr->has_multi_nodes()) { + _inst = mgr; + return _inst; + } + + delete mgr; + } +#endif /* LINUX */ + + _inst = new G1MemoryNodeManager(); + + return _inst; +} + +uint G1MemoryMultiNodeManager::index_of_node_id(int node_id) const { + assert(node_id >= 0, "invalid node id %d", node_id); + assert(node_id < _len_node_id_to_index_map, "invalid node id %d", node_id); + uint node_index = _node_id_to_index_map[node_id]; + assert(node_index != G1MemoryNodeManager::UnknownNodeIndex, + "invalid node id %d", node_id); + return node_index; +} + +G1MemoryMultiNodeManager::G1MemoryMultiNodeManager() : + _node_id_to_index_map(NULL), _len_node_id_to_index_map(0), _region_size(0), + _page_size(0), _node_ids(NULL), _num_active_node_ids(0) { + + assert(UseNUMA, "Invariant"); + + size_t num_node_ids = os::numa_get_groups_num(); + + _node_ids = NEW_C_HEAP_ARRAY(int, num_node_ids, mtGC); + _num_active_node_ids = (uint)os::numa_get_leaf_groups(_node_ids, num_node_ids); + + int max_node_id = 0; + for (uint i = 0; i < _num_active_node_ids; i++) { + if (_node_ids[i] > max_node_id) { + max_node_id = _node_ids[i]; + } + } + + _len_node_id_to_index_map = max_node_id + 1; + _node_id_to_index_map = NEW_C_HEAP_ARRAY(uint, _len_node_id_to_index_map, mtGC); + // Set all indices with unknown node id. + for (int i = 0; i < _len_node_id_to_index_map; i++) { + _node_id_to_index_map[i] = G1MemoryNodeManager::UnknownNodeIndex; + } + + // Set the indices for the actually retrieved node ids. + for (uint i = 0; i < _num_active_node_ids; i++) { + _node_id_to_index_map[_node_ids[i]] = i; + } +} + +G1MemoryMultiNodeManager::~G1MemoryMultiNodeManager() { + FREE_C_HEAP_ARRAY(int, _node_id_to_index_map); + FREE_C_HEAP_ARRAY(int, _node_ids); +} + +void G1MemoryMultiNodeManager::set_region_info(size_t region_size, size_t page_size) { + _region_size = region_size; + _page_size = page_size; +} + +uint G1MemoryMultiNodeManager::num_active_nodes() const { + assert(_num_active_node_ids > 0, "just checking"); + return _num_active_node_ids; +} + +const int* G1MemoryMultiNodeManager::node_ids() const { + return _node_ids; +} + +uint G1MemoryMultiNodeManager::index_of_current_thread() const { + int node_id = os::numa_get_group_id(); + return index_of_node_id(node_id); +} + +uint G1MemoryMultiNodeManager::preferred_node_index_for_index(uint region_index) const { + if (region_size() >= page_size()) { + // Simple case, pages are smaller than the region so we + // can just alternate over the nodes. + return region_index % _num_active_node_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() / region_size(); + return (region_index / regions_per_page) % _num_active_node_ids; + } +} + +uint G1MemoryMultiNodeManager::index_of_address(HeapWord* address) const { + int numa_id = os::numa_get_address_id((void*)address); + if (numa_id == os::InvalidId) { + return UnknownNodeIndex; + } else { + return index_of_node_id(numa_id); + } +} + +// Request to spread the given memory evenly across the available NUMA +// nodes. Which node to request for a given address is given by the +// region size and the page size. Below are two examples on 4 NUMA nodes system: +// 1. G1HeapRegionSize(_region_size) is larger than or equal to page size. +// * Page #: |-0--||-1--||-2--||-3--||-4--||-5--||-6--||-7--||-8--||-9--||-10-||-11-||-12-||-13-||-14-||-15-| +// * HeapRegion #: |----#0----||----#1----||----#2----||----#3----||----#4----||----#5----||----#6----||----#7----| +// * NUMA node #: |----#0----||----#1----||----#2----||----#3----||----#0----||----#1----||----#2----||----#3----| +// 2. G1HeapRegionSize(_region_size) is smaller than page size. +// Memory will be touched one page at a time because G1RegionToSpaceMapper commits +// pages one by one. +// * Page #: |-----0----||-----1----||-----2----||-----3----||-----4----||-----5----||-----6----||-----7----| +// * HeapRegion #: |-#0-||-#1-||-#2-||-#3-||-#4-||-#5-||-#6-||-#7-||-#8-||-#9-||#10-||#11-||#12-||#13-||#14-||#15-| +// * NUMA node #: |----#0----||----#1----||----#2----||----#3----||----#0----||----#1----||----#2----||----#3----| +void G1MemoryMultiNodeManager::request_memory_on_node(char* aligned_address, size_t size_in_bytes, uint node_index) { + 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; + } + + log_debug(gc, heap, numa)("Request memory [" PTR_FORMAT ", " PTR_FORMAT ") to be numa id (%d).", + p2i(aligned_address), p2i(aligned_address + size_in_bytes), _node_ids[node_index]); + os::numa_make_local(aligned_address, size_in_bytes, _node_ids[node_index]); +} + +const uint G1MemoryMultiNodeManager::max_search_depth() const { + // Multiple of 3 is just random number to limit iterations. + // There would be some cases that 1 page may be consisted of multiple HeapRegions. + return 3 * MAX2((uint)(page_size() / region_size()), (uint)1) * num_active_nodes(); +}