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 (must be signed) 35 typedef signed char chklvl_t; 36 37 #define CHKLVL_FORMAT "lv%.2d" 38 39 // Chunks are managed by a binary buddy allocator. 40 41 // Chunk sizes range from 1K to 4MB (64bit). 42 // 43 // Reasoning: .... TODO explain 44 45 // Each chunk has a level; the level corresponds to its position in the tree 46 // and describes its size. 47 // 48 // The largest chunks are called root chunks, of 4MB in size, and have level 0. 49 // From there on it goes: 50 // 51 // size level 52 // 4MB 0 53 // 2MB 1 54 // 1MB 2 55 // 512K 3 56 // 256K 4 57 // 128K 5 58 // 64K 6 59 // 32K 7 60 // 16K 8 61 // 8K 9 62 // 4K 10 63 // 2K 11 64 // 1K 12 65 66 namespace chklvl { 67 68 static const size_t MAX_CHUNK_BYTE_SIZE = 4 * M; 69 static const int NUM_CHUNK_LEVELS = 13; 70 static const size_t MIN_CHUNK_BYTE_SIZE = (MAX_CHUNK_BYTE_SIZE >> (size_t)NUM_CHUNK_LEVELS); 71 72 static const size_t MIN_CHUNK_WORD_SIZE = MIN_CHUNK_BYTE_SIZE / sizeof(MetaWord); 73 static const size_t MAX_CHUNK_WORD_SIZE = MAX_CHUNK_BYTE_SIZE / sizeof(MetaWord); 74 75 static const chklvl_t ROOT_CHUNK_LEVEL = 0; 76 77 static const chklvl_t HIGHEST_CHUNK_LEVEL = NUM_CHUNK_LEVELS - 1; 78 static const chklvl_t LOWEST_CHUNK_LEVEL = 0; 79 80 static const chklvl_t INVALID_CHUNK_LEVEL = (chklvl_t) -1; 81 82 inline bool is_valid_level(chklvl_t level) { 83 return level >= LOWEST_CHUNK_LEVEL && 84 level <= HIGHEST_CHUNK_LEVEL; 85 } 86 87 inline void check_valid_level(chklvl_t lvl) { 88 assert(is_valid_level(lvl), "invalid level (%d)", (int)lvl); 89 } 90 91 // Given a level return the chunk size, in words. 92 inline size_t word_size_for_level(chklvl_t level) { 93 check_valid_level(level); 94 return (MAX_CHUNK_BYTE_SIZE >> level) / BytesPerWord; 95 } 96 97 // Given an arbitrary word size smaller than the highest chunk size, 98 // return the highest chunk level able to hold this size. 99 // Returns INVALID_CHUNK_LEVEL if no fitting level can be found. 100 chklvl_t level_fitting_word_size(size_t word_size); 101 102 // Shorthands to refer to exact sizes 103 static const chklvl_t CHUNK_LEVEL_4M = ROOT_CHUNK_LEVEL; 104 static const chklvl_t CHUNK_LEVEL_2M = (ROOT_CHUNK_LEVEL + 1); 105 static const chklvl_t CHUNK_LEVEL_1M = (ROOT_CHUNK_LEVEL + 2); 106 static const chklvl_t CHUNK_LEVEL_512K = (ROOT_CHUNK_LEVEL + 3); 107 static const chklvl_t CHUNK_LEVEL_256K = (ROOT_CHUNK_LEVEL + 4); 108 static const chklvl_t CHUNK_LEVEL_128K = (ROOT_CHUNK_LEVEL + 5); 109 static const chklvl_t CHUNK_LEVEL_64K = (ROOT_CHUNK_LEVEL + 6); 110 static const chklvl_t CHUNK_LEVEL_32K = (ROOT_CHUNK_LEVEL + 7); 111 static const chklvl_t CHUNK_LEVEL_16K = (ROOT_CHUNK_LEVEL + 8); 112 static const chklvl_t CHUNK_LEVEL_8K = (ROOT_CHUNK_LEVEL + 9); 113 static const chklvl_t CHUNK_LEVEL_4K = (ROOT_CHUNK_LEVEL + 10); 114 static const chklvl_t CHUNK_LEVEL_2K = (ROOT_CHUNK_LEVEL + 11); 115 static const chklvl_t CHUNK_LEVEL_1K = (ROOT_CHUNK_LEVEL + 12); 116 117 STATIC_ASSERT(CHUNK_LEVEL_1K == HIGHEST_CHUNK_LEVEL); 118 119 120 } // namespace chklvl 121 122 } // namespace metaspace 123 124 #endif // SHARE_MEMORY_METASPACE_BLOCKFREELIST_HPP