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 "memory/metaspace/chunkManager.hpp"
28 #include "memory/metaspace/metaspaceSettings.hpp"
29 #include "metaspaceGtestCommon.hpp"
30 #include "metaspaceGtestContexts.hpp"
31
32 using metaspace::Settings;
33
checked_alloc_chunk_0(Metachunk ** p_return_value,chunklevel_t preferred_level,chunklevel_t max_level,size_t min_committed_size)34 void ChunkGtestContext::checked_alloc_chunk_0(Metachunk** p_return_value, chunklevel_t preferred_level, chunklevel_t max_level,
35 size_t min_committed_size) {
36
37 *p_return_value = NULL;
38
39 Metachunk* c = cm().get_chunk(preferred_level, max_level, min_committed_size);
40
41 if (c != NULL) {
42
43 ASSERT_LE(c->level(), max_level);
44 ASSERT_GE(c->level(), preferred_level);
45 ASSERT_GE(c->committed_words(), min_committed_size);
46 ASSERT_EQ(c->committed_words(), c->free_below_committed_words());
47 ASSERT_EQ(c->used_words(), (size_t)0);
48 ASSERT_TRUE(c->is_in_use());
49 ASSERT_FALSE(c->is_free());
50 ASSERT_FALSE(c->is_dead());
51 ASSERT_NULL(c->next());
52 ASSERT_NULL(c->prev());
53 if (c->level() == HIGHEST_CHUNK_LEVEL) {
54 ASSERT_TRUE(c->is_leaf_chunk());
55 } else {
56 ASSERT_FALSE(c->is_leaf_chunk());
57 }
58 if (c->level() == LOWEST_CHUNK_LEVEL) {
59 ASSERT_TRUE(c->is_root_chunk());
60 } else {
61 ASSERT_FALSE(c->is_root_chunk());
62 }
63 if (_num_chunks_allocated == 0) { // First chunk? We can make more assumptions
64 ASSERT_EQ(c->level(), preferred_level);
65 // Needs lock EXPECT_NULL(c->next_in_vs());
66 // Needs lock EXPECT_NULL(c->prev_in_vs());
67 ASSERT_TRUE(c->is_root_chunk() || c->is_leader());
68 }
69 if (Settings::new_chunks_are_fully_committed()) {
70 ASSERT_TRUE(c->is_fully_committed());
71 }
72
73 _num_chunks_allocated++;
74
75 }
76
77 *p_return_value = c;
78
79 }
80
81 // Test pattern established when allocating from the chunk with allocate_from_chunk_with_tests().
test_pattern(Metachunk * c,size_t word_size)82 void ChunkGtestContext::test_pattern(Metachunk* c, size_t word_size) {
83 check_range_for_pattern(c->base(), word_size, (uintx)c);
84 }
85
return_chunk(Metachunk * c)86 void ChunkGtestContext::return_chunk(Metachunk* c) {
87 test_pattern(c);
88 c->set_in_use(); // Forestall assert in cm
89 cm().return_chunk(c);
90 }
91
allocate_from_chunk(MetaWord ** p_return_value,Metachunk * c,size_t word_size)92 void ChunkGtestContext::allocate_from_chunk(MetaWord** p_return_value, Metachunk* c, size_t word_size) {
93
94 size_t used_before = c->used_words();
95 size_t free_before = c->free_words();
96 size_t free_below_committed_before = c->free_below_committed_words();
97 const MetaWord* top_before = c->top();
98
99 MetaWord* p = c->allocate(word_size);
100 EXPECT_NOT_NULL(p);
101 EXPECT_EQ(c->used_words(), used_before + word_size);
102 EXPECT_EQ(c->free_words(), free_before - word_size);
103 EXPECT_EQ(c->free_below_committed_words(), free_below_committed_before - word_size);
104 EXPECT_EQ(c->top(), top_before + word_size);
105
106 // Old content should be preserved
107 test_pattern(c, used_before);
108
109 // Fill newly allocated range too
110 fill_range_with_pattern(p, word_size, (uintx)c);
111
112 *p_return_value = p;
113 }
114
commit_chunk_with_test(Metachunk * c,size_t additional_size)115 void ChunkGtestContext::commit_chunk_with_test(Metachunk* c, size_t additional_size) {
116
117 size_t used_before = c->used_words();
118 size_t free_before = c->free_words();
119 const MetaWord* top_before = c->top();
120
121 c->set_in_use();
122 bool b = c->ensure_committed_additional(additional_size);
123 EXPECT_TRUE(b);
124
125 // We should have enough committed size now
126 EXPECT_GE(c->free_below_committed_words(), additional_size);
127
128 // used, free, top should be unchanged.
129 EXPECT_EQ(c->used_words(), used_before);
130 EXPECT_EQ(c->free_words(), free_before);
131 EXPECT_EQ(c->top(), top_before);
132
133 test_pattern(c, used_before);
134
135 }
136
commit_chunk_expect_failure(Metachunk * c,size_t additional_size)137 void ChunkGtestContext::commit_chunk_expect_failure(Metachunk* c, size_t additional_size) {
138
139 size_t used_before = c->used_words();
140 size_t free_before = c->free_words();
141 size_t free_below_committed_before = c->free_below_committed_words();
142 const MetaWord* top_before = c->top();
143
144 c->set_in_use();
145 bool b = c->ensure_committed_additional(additional_size);
146 EXPECT_FALSE(b);
147
148 // Nothing should have changed
149 EXPECT_EQ(c->used_words(), used_before);
150 EXPECT_EQ(c->free_words(), free_before);
151 EXPECT_EQ(c->free_below_committed_words(), free_below_committed_before);
152 EXPECT_EQ(c->top(), top_before);
153
154 test_pattern(c, used_before);
155
156 }
157
uncommit_chunk_with_test(Metachunk * c)158 void ChunkGtestContext::uncommit_chunk_with_test(Metachunk* c) {
159 if (c->word_size() >= Settings::commit_granule_words()) {
160 c->set_free(); // Forestall assert in uncommit
161 c->reset_used_words();
162 c->uncommit();
163
164 EXPECT_EQ(c->free_below_committed_words(), (size_t)0);
165 EXPECT_EQ(c->used_words(), (size_t)0);
166 EXPECT_EQ(c->free_words(), c->word_size());
167 EXPECT_EQ(c->top(), c->base());
168 EXPECT_TRUE(c->is_fully_uncommitted());
169 }
170 }
171
172 /////// SparseArray<T> ////////////////
173
174