1 /* 2 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #ifndef SHARE_MEMORY_METASPACE_CHUNKLEVEL_HPP 26 #define SHARE_MEMORY_METASPACE_CHUNKLEVEL_HPP 27 28 #include "utilities/globalDefinitions.hpp" 29 30 // Constants for the chunk levels and some utility functions. 31 32 namespace metaspace { 33 34 // Metachunk level 35 typedef unsigned chklvl_t; 36 37 // Chunks are managed by a quaternary buddy allocator (allowing four empty 38 // chunks to be combined to a larger chunk). 39 40 // Chunk sizes range from 1K to 4MB (64bit). 41 // 42 // Reasoning: .... TODO explain 43 44 // We use a quaternary buddy allocator instead of a simple binary one because 45 // binary trees would get too deep and require too much memory for the nodes; 46 // A 1:4 merge strategy is enough for our causes, and keeps the costs for the 47 // tree nodes cheap. 48 49 // Each chunk has a level; the level corresponds to its position in the tree 50 // and describes its size. 51 // 52 // The largest chunks are called root chunks, of 4MB in size, and have level 0. 53 // From there on it goes: 54 // 55 // size level 56 // 4MB 0 57 // 1MB 1 58 // 256K 2 59 // 64K 3 60 // 16K 4 61 // 4K 5 62 // 1K 6 63 64 namespace chklvl { 65 66 static const size_t MAX_CHUNK_BYTE_SIZE = 4 * M; 67 static const int NUM_CHUNK_LEVELS = 7; 68 static const size_t MIN_CHUNK_BYTE_SIZE = (MAX_CHUNK_BYTE_SIZE >> (size_t)NUM_CHUNK_LEVELS); 69 70 static const size_t MIN_CHUNK_WORD_SIZE = MIN_CHUNK_BYTE_SIZE / sizeof(MetaWord); 71 static const size_t MAX_CHUNK_WORD_SIZE = MAX_CHUNK_BYTE_SIZE / sizeof(MetaWord); 72 73 static const chklvl_t ROOT_CHUNK_LEVEL = 0; 74 75 static const chklvl_t HIGHEST_CHUNK_LEVEL = NUM_CHUNK_LEVELS - 1; 76 static const chklvl_t LOWEST_CHUNK_LEVEL = 0; 77 78 inline bool is_valid_level(chklvl_t level) { 79 return level <= HIGHEST_CHUNK_LEVEL; 80 } 81 82 inline void check_valid_level(chklvl_t lvl) { 83 assert(is_valid_level(lvl), "invalid level (%d)", (int)lvl); 84 } 85 86 // Given a level return the chunk size, in words. 87 inline size_t word_size_for_level(chklvl_t level) { 88 assert(is_valid_level(level), "invalid chunk level (%d)", level); 89 return MAX_CHUNK_BYTE_SIZE >> level; 90 } 91 92 // Given an arbitrary word size smaller than the highest chunk size, 93 // return the highest chunk level able to hold this size. 94 chklvl_t level_fitting_word_size(size_t word_size); 95 96 // Shorthands to refer to exact sizes 97 const chklvl_t CHUNK_LEVEL_4M = ROOT_CHUNK_LEVEL; 98 const chklvl_t CHUNK_LEVEL_1M = (ROOT_CHUNK_LEVEL + 1); 99 const chklvl_t CHUNK_LEVEL_256K = (ROOT_CHUNK_LEVEL + 2); 100 const chklvl_t CHUNK_LEVEL_64K = (ROOT_CHUNK_LEVEL + 3); 101 const chklvl_t CHUNK_LEVEL_16K = (ROOT_CHUNK_LEVEL + 4); 102 const chklvl_t CHUNK_LEVEL_4K = (ROOT_CHUNK_LEVEL + 5); 103 const chklvl_t CHUNK_LEVEL_1K = (ROOT_CHUNK_LEVEL + 6);; 104 105 STATIC_ASSERT(CHUNK_LEVEL_1K == HIGHEST_CHUNK_LEVEL); 106 107 } // namespace chklvl 108 109 } // namespace metaspace 110 111 #endif // SHARE_MEMORY_METASPACE_BLOCKFREELIST_HPP