jdk

Форк
0
/z
/
test_zMapper_windows.cpp 
188 строк · 5.3 Кб
1
/*
2
 * Copyright (c) 2024, 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
#include "precompiled.hpp"
25

26
#ifdef _WINDOWS
27

28
#include "gc/z/zAddress.inline.hpp"
29
#include "gc/z/zGlobals.hpp"
30
#include "gc/z/zList.inline.hpp"
31
#include "gc/z/zMemory.inline.hpp"
32
#include "gc/z/zSyscall_windows.hpp"
33
#include "gc/z/zVirtualMemory.hpp"
34
#include "runtime/os.hpp"
35
#include "unittest.hpp"
36

37
using namespace testing;
38

39
#define EXPECT_ALLOC_OK(offset) EXPECT_NE(offset, zoffset(UINTPTR_MAX))
40

41
class ZMapperTest : public Test {
42
private:
43
  static constexpr size_t ZMapperTestReservationSize = 32 * M;
44

45
  static bool            _initialized;
46
  static ZMemoryManager* _va;
47

48
  ZVirtualMemoryManager* _vmm;
49

50
public:
51
  bool reserve_for_test() {
52
    // Initialize platform specific parts before reserving address space
53
    _vmm->pd_initialize_before_reserve();
54

55
    // Reserve address space
56
    if (!_vmm->pd_reserve(ZOffset::address_unsafe(zoffset(0)), ZMapperTestReservationSize)) {
57
      return false;
58
    }
59

60
    // Make the address range free before setting up callbacks below
61
    _va->free(zoffset(0), ZMapperTestReservationSize);
62

63
    // Initialize platform specific parts after reserving address space
64
    _vmm->pd_initialize_after_reserve();
65

66
    return true;
67
  }
68

69
  virtual void SetUp() {
70
    // Only run test on supported Windows versions
71
    if (!ZSyscall::is_supported()) {
72
      GTEST_SKIP() << "Requires Windows version 1803 or later";
73
      return;
74
    }
75

76
    ZSyscall::initialize();
77
    ZGlobalsPointers::initialize();
78

79
    // Fake a ZVirtualMemoryManager
80
    _vmm = (ZVirtualMemoryManager*)os::malloc(sizeof(ZVirtualMemoryManager), mtTest);
81

82
    // Construct its internal ZMemoryManager
83
    _va = new (&_vmm->_manager) ZMemoryManager();
84

85
    // Reserve address space for the test
86
    if (!reserve_for_test()) {
87
      GTEST_SKIP() << "Failed to reserve address space";
88
      return;
89
    }
90

91
    _initialized = true;
92
  }
93

94
  virtual void TearDown() {
95
    if (!ZSyscall::is_supported()) {
96
      // Test skipped, nothing to cleanup
97
      return;
98
    }
99

100
    if (_initialized) {
101
      _vmm->pd_unreserve(ZOffset::address_unsafe(zoffset(0)), 0);
102
    }
103
    os::free(_vmm);
104
  }
105

106
  static void test_alloc_low_address() {
107
    // Verify that we get placeholder for first granule
108
    zoffset bottom = _va->alloc_low_address(ZGranuleSize);
109
    EXPECT_ALLOC_OK(bottom);
110

111
    _va->free(bottom, ZGranuleSize);
112

113
    // Alloc something larger than a granule and free it
114
    bottom = _va->alloc_low_address(ZGranuleSize * 3);
115
    EXPECT_ALLOC_OK(bottom);
116

117
    _va->free(bottom, ZGranuleSize * 3);
118

119
    // Free with more memory allocated
120
    bottom = _va->alloc_low_address(ZGranuleSize);
121
    EXPECT_ALLOC_OK(bottom);
122

123
    zoffset next = _va->alloc_low_address(ZGranuleSize);
124
    EXPECT_ALLOC_OK(next);
125

126
    _va->free(bottom, ZGranuleSize);
127
    _va->free(next, ZGranuleSize);
128
  }
129

130
  static void test_alloc_high_address() {
131
    // Verify that we get placeholder for last granule
132
    zoffset high = _va->alloc_high_address(ZGranuleSize);
133
    EXPECT_ALLOC_OK(high);
134

135
    zoffset prev = _va->alloc_high_address(ZGranuleSize);
136
    EXPECT_ALLOC_OK(prev);
137

138
    _va->free(high, ZGranuleSize);
139
    _va->free(prev, ZGranuleSize);
140

141
    // Alloc something larger than a granule and return it
142
    high = _va->alloc_high_address(ZGranuleSize * 2);
143
    EXPECT_ALLOC_OK(high);
144

145
    _va->free(high, ZGranuleSize * 2);
146
  }
147

148
  static void test_alloc_whole_area() {
149
    // Alloc the whole reservation
150
    zoffset bottom = _va->alloc_low_address(ZMapperTestReservationSize);
151
    EXPECT_ALLOC_OK(bottom);
152

153
    // Free two chunks and then allocate them again
154
    _va->free(bottom, ZGranuleSize * 4);
155
    _va->free(bottom + ZGranuleSize * 6, ZGranuleSize * 6);
156

157
    zoffset offset = _va->alloc_low_address(ZGranuleSize * 4);
158
    EXPECT_ALLOC_OK(offset);
159

160
    offset = _va->alloc_low_address(ZGranuleSize * 6);
161
    EXPECT_ALLOC_OK(offset);
162

163
    // Now free it all, and verify it can be re-allocated
164
    _va->free(bottom, ZMapperTestReservationSize);
165

166
    bottom = _va->alloc_low_address(ZMapperTestReservationSize);
167
    EXPECT_ALLOC_OK(bottom);
168

169
    _va->free(bottom, ZMapperTestReservationSize);
170
  }
171
};
172

173
bool ZMapperTest::_initialized   = false;
174
ZMemoryManager* ZMapperTest::_va = nullptr;
175

176
TEST_VM_F(ZMapperTest, test_alloc_low_address) {
177
  test_alloc_low_address();
178
}
179

180
TEST_VM_F(ZMapperTest, test_alloc_high_address) {
181
  test_alloc_high_address();
182
}
183

184
TEST_VM_F(ZMapperTest, test_alloc_whole_area) {
185
  test_alloc_whole_area();
186
}
187

188
#endif // _WINDOWS
189

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.