jdk

Форк
0
/x
/
test_xForwarding.cpp 
205 строк · 6.5 Кб
1
/*
2
 * Copyright (c) 2016, 2020, 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
#include "gc/x/xAddress.inline.hpp"
26
#include "gc/x/xForwarding.inline.hpp"
27
#include "gc/x/xForwardingAllocator.inline.hpp"
28
#include "gc/x/xGlobals.hpp"
29
#include "gc/x/xPage.inline.hpp"
30
#include "unittest.hpp"
31

32
using namespace testing;
33

34
#define CAPTURE_DELIM "\n"
35
#define CAPTURE1(expression) #expression << " evaluates to " << expression
36
#define CAPTURE2(e0, e1)                 CAPTURE1(e0) << CAPTURE_DELIM << CAPTURE1(e1)
37

38
#define CAPTURE(expression) CAPTURE1(expression)
39

40
class XForwardingTest : public Test {
41
public:
42
  // Helper functions
43

44
  class SequenceToFromIndex : AllStatic {
45
  public:
46
    static uintptr_t even(size_t sequence_number) {
47
      return sequence_number * 2;
48
    }
49
    static uintptr_t odd(size_t sequence_number) {
50
      return even(sequence_number) + 1;
51
    }
52
    static uintptr_t one_to_one(size_t sequence_number) {
53
      return sequence_number;
54
    }
55
  };
56

57
  // Test functions
58

59
  static void setup(XForwarding* forwarding) {
60
    EXPECT_PRED1(is_power_of_2<size_t>, forwarding->_entries.length()) << CAPTURE(forwarding->_entries.length());
61
  }
62

63
  static void find_empty(XForwarding* forwarding) {
64
    size_t size = forwarding->_entries.length();
65
    size_t entries_to_check = size * 2;
66

67
    for (size_t i = 0; i < entries_to_check; i++) {
68
      uintptr_t from_index = SequenceToFromIndex::one_to_one(i);
69

70
      XForwardingCursor cursor;
71
      XForwardingEntry entry = forwarding->find(from_index, &cursor);
72
      EXPECT_FALSE(entry.populated()) << CAPTURE2(from_index, size);
73
    }
74
  }
75

76
  static void find_full(XForwarding* forwarding) {
77
    size_t size = forwarding->_entries.length();
78
    size_t entries_to_populate = size;
79

80
    // Populate
81
    for (size_t i = 0; i < entries_to_populate; i++) {
82
      uintptr_t from_index = SequenceToFromIndex::one_to_one(i);
83

84
      XForwardingCursor cursor;
85
      XForwardingEntry entry = forwarding->find(from_index, &cursor);
86
      ASSERT_FALSE(entry.populated()) << CAPTURE2(from_index, size);
87

88
      forwarding->insert(from_index, from_index, &cursor);
89
    }
90

91
    // Verify
92
    for (size_t i = 0; i < entries_to_populate; i++) {
93
      uintptr_t from_index = SequenceToFromIndex::one_to_one(i);
94

95
      XForwardingCursor cursor;
96
      XForwardingEntry entry = forwarding->find(from_index, &cursor);
97
      ASSERT_TRUE(entry.populated()) << CAPTURE2(from_index, size);
98

99
      ASSERT_EQ(entry.from_index(), from_index) << CAPTURE(size);
100
      ASSERT_EQ(entry.to_offset(), from_index) << CAPTURE(size);
101
    }
102
  }
103

104
  static void find_every_other(XForwarding* forwarding) {
105
    size_t size = forwarding->_entries.length();
106
    size_t entries_to_populate = size / 2;
107

108
    // Populate even from indices
109
    for (size_t i = 0; i < entries_to_populate; i++) {
110
      uintptr_t from_index = SequenceToFromIndex::even(i);
111

112
      XForwardingCursor cursor;
113
      XForwardingEntry entry = forwarding->find(from_index, &cursor);
114
      ASSERT_FALSE(entry.populated()) << CAPTURE2(from_index, size);
115

116
      forwarding->insert(from_index, from_index, &cursor);
117
    }
118

119
    // Verify populated even indices
120
    for (size_t i = 0; i < entries_to_populate; i++) {
121
      uintptr_t from_index = SequenceToFromIndex::even(i);
122

123
      XForwardingCursor cursor;
124
      XForwardingEntry entry = forwarding->find(from_index, &cursor);
125
      ASSERT_TRUE(entry.populated()) << CAPTURE2(from_index, size);
126

127
      ASSERT_EQ(entry.from_index(), from_index) << CAPTURE(size);
128
      ASSERT_EQ(entry.to_offset(), from_index) << CAPTURE(size);
129
    }
130

131
    // Verify empty odd indices
132
    //
133
    // This check could be done on a larger range of sequence numbers,
134
    // but currently entries_to_populate is used.
135
    for (size_t i = 0; i < entries_to_populate; i++) {
136
      uintptr_t from_index = SequenceToFromIndex::odd(i);
137

138
      XForwardingCursor cursor;
139
      XForwardingEntry entry = forwarding->find(from_index, &cursor);
140

141
      ASSERT_FALSE(entry.populated()) << CAPTURE2(from_index, size);
142
    }
143
  }
144

145
  static void test(void (*function)(XForwarding*), uint32_t size) {
146
    // Create page
147
    const XVirtualMemory vmem(0, XPageSizeSmall);
148
    const XPhysicalMemory pmem(XPhysicalMemorySegment(0, XPageSizeSmall, true));
149
    XPage page(XPageTypeSmall, vmem, pmem);
150

151
    page.reset();
152

153
    const size_t object_size = 16;
154
    const uintptr_t object = page.alloc_object(object_size);
155

156
    XGlobalSeqNum++;
157

158
    bool dummy = false;
159
    page.mark_object(XAddress::marked(object), dummy, dummy);
160

161
    const uint32_t live_objects = size;
162
    const size_t live_bytes = live_objects * object_size;
163
    page.inc_live(live_objects, live_bytes);
164

165
    // Setup allocator
166
    XForwardingAllocator allocator;
167
    const uint32_t nentries = XForwarding::nentries(&page);
168
    allocator.reset((sizeof(XForwarding)) + (nentries * sizeof(XForwardingEntry)));
169

170
    // Setup forwarding
171
    XForwarding* const forwarding = XForwarding::alloc(&allocator, &page);
172

173
    // Actual test function
174
    (*function)(forwarding);
175
  }
176

177
  // Run the given function with a few different input values.
178
  static void test(void (*function)(XForwarding*)) {
179
    test(function, 1);
180
    test(function, 2);
181
    test(function, 3);
182
    test(function, 4);
183
    test(function, 7);
184
    test(function, 8);
185
    test(function, 1023);
186
    test(function, 1024);
187
    test(function, 1025);
188
  }
189
};
190

191
TEST_F(XForwardingTest, setup) {
192
  test(&XForwardingTest::setup);
193
}
194

195
TEST_F(XForwardingTest, find_empty) {
196
  test(&XForwardingTest::find_empty);
197
}
198

199
TEST_F(XForwardingTest, find_full) {
200
  test(&XForwardingTest::find_full);
201
}
202

203
TEST_F(XForwardingTest, find_every_other) {
204
  test(&XForwardingTest::find_every_other);
205
}
206

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

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

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

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