1/*
2 * Copyright (c) 2016, 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 "runtime/os.hpp"
26#include "unittest.hpp"
27#include "utilities/globalDefinitions.hpp"
28
29static ::testing::AssertionResult testPageAddress(
30 const char* expected_addr_expr,
31 const char* addr_expr,
32 const char* page_addr_expr,
33 const char* page_size_expr,
34 const char* actual_addr_expr,
35 address expected_addr,
36 address addr,
37 address page_addr,
38 intptr_t page_size,
39 address actual_addr) {
40 if (expected_addr == actual_addr) {
41 return ::testing::AssertionSuccess();
42 }
43
44 return ::testing::AssertionFailure()
45 << actual_addr_expr << " returned unexpected address " << (void*) actual_addr << std::endl
46 << "Expected " << expected_addr_expr << ": " << (void*) expected_addr << std::endl
47 << "where" << std::endl
48 << addr_expr << ": " << (void*) addr << std::endl
49 << page_addr_expr << ": " << (void*) page_addr << std::endl
50 << page_size_expr << ": " << page_size;
51}
52
53TEST_VM(globalDefinitions, clamp_address_in_page) {
54 const intptr_t page_sizes[] = {os::vm_page_size(), 4096, 8192, 65536, 2 * 1024 * 1024};
55 const int num_page_sizes = sizeof(page_sizes) / sizeof(page_sizes[0]);
56
57 for (int i = 0; i < num_page_sizes; i++) {
58 intptr_t page_size = page_sizes[i];
59 address page_address = (address) (10 * page_size);
60
61 const intptr_t within_page_offsets[] = {0, 128, page_size - 1};
62 const int num_within_page_offsets = sizeof(within_page_offsets) / sizeof(within_page_offsets[0]);
63
64 for (int k = 0; k < num_within_page_offsets; ++k) {
65 address addr = page_address + within_page_offsets[k];
66 address expected_address = addr;
67 EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,
68 clamp_address_in_page(addr, page_address, page_size))
69 << "Expect that address within page is returned as is";
70 }
71
72 const intptr_t above_page_offsets[] = {page_size, page_size + 1, 5 * page_size + 1};
73 const int num_above_page_offsets = sizeof(above_page_offsets) / sizeof(above_page_offsets[0]);
74
75 for (int k = 0; k < num_above_page_offsets; ++k) {
76 address addr = page_address + above_page_offsets[k];
77 address expected_address = page_address + page_size;
78 EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,
79 clamp_address_in_page(addr, page_address, page_size))
80 << "Expect that address above page returns start of next page";
81 }
82
83 const intptr_t below_page_offsets[] = {1, 2 * page_size + 1, 5 * page_size + 1};
84 const int num_below_page_offsets = sizeof(below_page_offsets) / sizeof(below_page_offsets[0]);
85
86 for (int k = 0; k < num_below_page_offsets; ++k) {
87 address addr = page_address - below_page_offsets[k];
88 address expected_address = page_address;
89 EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size,
90 clamp_address_in_page(addr, page_address, page_size))
91 << "Expect that address below page returns start of page";
92 }
93 }
94}
95
96TEST(globalDefinitions, proper_unit) {
97 EXPECT_EQ(0u, byte_size_in_proper_unit(0u));
98 EXPECT_STREQ("B", proper_unit_for_byte_size(0u));
99
100 EXPECT_EQ(1u, byte_size_in_proper_unit(1u));
101 EXPECT_STREQ("B", proper_unit_for_byte_size(1u));
102
103 EXPECT_EQ(1023u, byte_size_in_proper_unit(K - 1));
104 EXPECT_STREQ("B", proper_unit_for_byte_size(K - 1));
105
106 EXPECT_EQ(1024u, byte_size_in_proper_unit(K));
107 EXPECT_STREQ("B", proper_unit_for_byte_size(K));
108
109 EXPECT_EQ(1025u, byte_size_in_proper_unit(K + 1));
110 EXPECT_STREQ("B", proper_unit_for_byte_size(K + 1));
111
112 EXPECT_EQ(51200u, byte_size_in_proper_unit(50*K));
113 EXPECT_STREQ("B", proper_unit_for_byte_size(50*K));
114
115 EXPECT_EQ(1023u, byte_size_in_proper_unit(M - 1));
116 EXPECT_STREQ("K", proper_unit_for_byte_size(M - 1));
117
118 EXPECT_EQ(1024u, byte_size_in_proper_unit(M));
119 EXPECT_STREQ("K", proper_unit_for_byte_size(M));
120
121 EXPECT_EQ(1024u, byte_size_in_proper_unit(M + 1));
122 EXPECT_STREQ("K", proper_unit_for_byte_size(M + 1));
123
124 EXPECT_EQ(1025u, byte_size_in_proper_unit(M + K));
125 EXPECT_STREQ("K", proper_unit_for_byte_size(M + K));
126
127 EXPECT_EQ(51200u, byte_size_in_proper_unit(50*M));
128 EXPECT_STREQ("K", proper_unit_for_byte_size(50*M));
129
130#ifdef _LP64
131 EXPECT_EQ(1023u, byte_size_in_proper_unit(G - 1));
132 EXPECT_STREQ("M", proper_unit_for_byte_size(G - 1));
133
134 EXPECT_EQ(1024u, byte_size_in_proper_unit(G));
135 EXPECT_STREQ("M", proper_unit_for_byte_size(G));
136
137 EXPECT_EQ(1024u, byte_size_in_proper_unit(G + 1));
138 EXPECT_STREQ("M", proper_unit_for_byte_size(G + 1));
139
140 EXPECT_EQ(1024u, byte_size_in_proper_unit(G + K));
141 EXPECT_STREQ("M", proper_unit_for_byte_size(G + K));
142
143 EXPECT_EQ(1025u, byte_size_in_proper_unit(G + M));
144 EXPECT_STREQ("M", proper_unit_for_byte_size(G + M));
145
146 EXPECT_EQ(51200u, byte_size_in_proper_unit(50*G));
147 EXPECT_STREQ("M", proper_unit_for_byte_size(50*G));
148#endif
149}
150
151TEST(globalDefinitions, exact_unit_for_byte_size) {
152 EXPECT_STREQ("B", exact_unit_for_byte_size(0));
153 EXPECT_STREQ("B", exact_unit_for_byte_size(1));
154 EXPECT_STREQ("B", exact_unit_for_byte_size(K - 1));
155 EXPECT_STREQ("K", exact_unit_for_byte_size(K));
156 EXPECT_STREQ("B", exact_unit_for_byte_size(K + 1));
157 EXPECT_STREQ("B", exact_unit_for_byte_size(M - 1));
158 EXPECT_STREQ("M", exact_unit_for_byte_size(M));
159 EXPECT_STREQ("B", exact_unit_for_byte_size(M + 1));
160 EXPECT_STREQ("K", exact_unit_for_byte_size(M + K));
161#ifdef _LP64
162 EXPECT_STREQ("B", exact_unit_for_byte_size(G - 1));
163 EXPECT_STREQ("G", exact_unit_for_byte_size(G));
164 EXPECT_STREQ("B", exact_unit_for_byte_size(G + 1));
165 EXPECT_STREQ("K", exact_unit_for_byte_size(G + K));
166 EXPECT_STREQ("M", exact_unit_for_byte_size(G + M));
167 EXPECT_STREQ("K", exact_unit_for_byte_size(G + M + K));
168#endif
169}
170
171TEST(globalDefinitions, byte_size_in_exact_unit) {
172 EXPECT_EQ(0u, byte_size_in_exact_unit(0));
173 EXPECT_EQ(1u, byte_size_in_exact_unit(1));
174 EXPECT_EQ(K - 1, byte_size_in_exact_unit(K - 1));
175 EXPECT_EQ(1u, byte_size_in_exact_unit(K));
176 EXPECT_EQ(K + 1, byte_size_in_exact_unit(K + 1));
177 EXPECT_EQ(M - 1, byte_size_in_exact_unit(M - 1));
178 EXPECT_EQ(1u, byte_size_in_exact_unit(M));
179 EXPECT_EQ(M + 1, byte_size_in_exact_unit(M + 1));
180 EXPECT_EQ(K + 1, byte_size_in_exact_unit(M + K));
181#ifdef _LP64
182 EXPECT_EQ(G - 1, byte_size_in_exact_unit(G - 1));
183 EXPECT_EQ(1u, byte_size_in_exact_unit(G));
184 EXPECT_EQ(G + 1, byte_size_in_exact_unit(G + 1));
185 EXPECT_EQ(M + 1, byte_size_in_exact_unit(G + K));
186 EXPECT_EQ(K + 1, byte_size_in_exact_unit(G + M));
187 EXPECT_EQ(M + K + 1, byte_size_in_exact_unit(G + M + K));
188#endif
189}
190