1 /*
2  * Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3  * Copyright (c) 2020 SAP SE. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.
9  *
10  * This code is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13  * version 2 for more details (a copy is included in the LICENSE file that
14  * accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License version
17  * 2 along with this work; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19  *
20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21  * or visit www.oracle.com if you need additional information or have any
22  * questions.
23  *
24  */
25 
26 #include "precompiled.hpp"
27 #include "classfile/classLoaderData.hpp"
28 #include "memory/classLoaderMetaspace.hpp"
29 #include "memory/metaspace/chunklevel.hpp"
30 #include "memory/metaspace/metaspaceSettings.hpp"
31 #include "utilities/powerOfTwo.hpp"
32 // #define LOG_PLEASE
33 #include "metaspaceGtestCommon.hpp"
34 
35 using metaspace::chunklevel_t;
36 using namespace metaspace::chunklevel;
37 using metaspace::Settings;
38 
TEST_VM(metaspace,misc_sizes)39 TEST_VM(metaspace, misc_sizes)   {
40 
41   // Test test common sizes (seems primitive but breaks surprisingly often during development
42   //  because of word vs byte confusion)
43   // Adjust this test if numbers change.
44   ASSERT_TRUE(Settings::commit_granule_bytes() == 16 * K ||
45               Settings::commit_granule_bytes() == 64 * K);
46   ASSERT_EQ(Settings::commit_granule_bytes(), Metaspace::commit_alignment());
47   ASSERT_TRUE(is_aligned(Settings::virtual_space_node_default_word_size(),
48               metaspace::chunklevel::MAX_CHUNK_WORD_SIZE));
49   ASSERT_EQ(Settings::virtual_space_node_default_word_size(),
50             metaspace::chunklevel::MAX_CHUNK_WORD_SIZE * 2);
51   ASSERT_EQ(Settings::virtual_space_node_reserve_alignment_words(),
52             Metaspace::reserve_alignment_words());
53 
54 }
55 
TEST_VM(metaspace,misc_max_alloc_size)56 TEST_VM(metaspace, misc_max_alloc_size)   {
57 
58   // Make sure we can allocate what we promise to allocate
59   const size_t sz = Metaspace::max_allocation_word_size();
60   ClassLoaderData* cld = ClassLoaderData::the_null_class_loader_data();
61   MetaWord* p = cld->metaspace_non_null()->allocate(sz, Metaspace::NonClassType);
62   ASSERT_NOT_NULL(p);
63   cld->metaspace_non_null()->deallocate(p, sz, false);
64 
65 }
66 
TEST_VM(metaspace,chunklevel_utils)67 TEST_VM(metaspace, chunklevel_utils)   {
68 
69   // These tests seem to be really basic, but it is amazing what one can
70   // break accidentally...
71   LOG(SIZE_FORMAT, MAX_CHUNK_BYTE_SIZE);
72   LOG(SIZE_FORMAT, MIN_CHUNK_BYTE_SIZE);
73   LOG(SIZE_FORMAT, MIN_CHUNK_WORD_SIZE);
74   LOG(SIZE_FORMAT, MAX_CHUNK_WORD_SIZE);
75   LOG(SIZE_FORMAT, MAX_CHUNK_BYTE_SIZE);
76   LOG("%u", (unsigned)ROOT_CHUNK_LEVEL);
77   LOG("%u", (unsigned)HIGHEST_CHUNK_LEVEL);
78   LOG("%u", (unsigned)LOWEST_CHUNK_LEVEL);
79 
80   static const chunklevel_t INVALID_CHUNK_LEVEL    = (chunklevel_t) -1;
81 
82   EXPECT_TRUE(is_power_of_2(MAX_CHUNK_WORD_SIZE));
83   EXPECT_TRUE(is_power_of_2(MIN_CHUNK_WORD_SIZE));
84 
85   EXPECT_TRUE(is_valid_level(LOWEST_CHUNK_LEVEL));
86   EXPECT_TRUE(is_valid_level(HIGHEST_CHUNK_LEVEL));
87   EXPECT_FALSE(is_valid_level(HIGHEST_CHUNK_LEVEL + 1));
88   EXPECT_FALSE(is_valid_level(LOWEST_CHUNK_LEVEL - 1));
89 
90   EXPECT_EQ(word_size_for_level(ROOT_CHUNK_LEVEL), MAX_CHUNK_WORD_SIZE);
91   EXPECT_EQ(word_size_for_level(HIGHEST_CHUNK_LEVEL), MIN_CHUNK_WORD_SIZE);
92 
93   EXPECT_EQ(word_size_for_level(CHUNK_LEVEL_4K), (4 * K) / BytesPerWord);
94   EXPECT_EQ(word_size_for_level(CHUNK_LEVEL_64K), (64 * K) / BytesPerWord);
95 
96   EXPECT_EQ(level_fitting_word_size(0), HIGHEST_CHUNK_LEVEL);
97   EXPECT_EQ(level_fitting_word_size(1), HIGHEST_CHUNK_LEVEL);
98   EXPECT_EQ(level_fitting_word_size(MIN_CHUNK_WORD_SIZE), HIGHEST_CHUNK_LEVEL);
99   EXPECT_EQ(level_fitting_word_size(MIN_CHUNK_WORD_SIZE + 1), HIGHEST_CHUNK_LEVEL - 1);
100 
101   EXPECT_EQ(level_fitting_word_size(MAX_CHUNK_WORD_SIZE), ROOT_CHUNK_LEVEL);
102   EXPECT_EQ(level_fitting_word_size(MAX_CHUNK_WORD_SIZE - 1), ROOT_CHUNK_LEVEL);
103   EXPECT_EQ(level_fitting_word_size((MAX_CHUNK_WORD_SIZE / 2) + 1), ROOT_CHUNK_LEVEL);
104   EXPECT_EQ(level_fitting_word_size(MAX_CHUNK_WORD_SIZE / 2), ROOT_CHUNK_LEVEL + 1);
105 
106   EXPECT_EQ(level_fitting_word_size(8 * K), LP64_ONLY(CHUNK_LEVEL_64K) NOT_LP64(CHUNK_LEVEL_32K));
107   EXPECT_EQ(level_fitting_word_size(8 * K + 13), LP64_ONLY(CHUNK_LEVEL_64K) NOT_LP64(CHUNK_LEVEL_32K) - 1);
108   EXPECT_EQ(level_fitting_word_size(8 * K - 13), LP64_ONLY(CHUNK_LEVEL_64K) NOT_LP64(CHUNK_LEVEL_32K));
109 
110 }
111 
112