author | coleenp |
Wed, 28 Jun 2017 16:14:20 -0400 | |
changeset 46587 | 6c97f34cb194 |
parent 42056 | 9296564b8c8b |
permissions | -rw-r--r-- |
38255 | 1 |
/* |
46587
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
2 |
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
38255 | 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 |
||
39394 | 24 |
#include "precompiled.hpp" |
38255 | 25 |
#include "runtime/os.hpp" |
26 |
#include "unittest.hpp" |
|
27 |
||
42056 | 28 |
static size_t small_page_size() { |
29 |
return os::vm_page_size(); |
|
30 |
} |
|
31 |
||
32 |
static size_t large_page_size() { |
|
33 |
const size_t large_page_size_example = 4 * M; |
|
34 |
return os::page_size_for_region_aligned(large_page_size_example, 1); |
|
35 |
} |
|
36 |
||
38255 | 37 |
TEST_VM(os, page_size_for_region) { |
38 |
size_t large_page_example = 4 * M; |
|
39 |
size_t large_page = os::page_size_for_region_aligned(large_page_example, 1); |
|
40 |
||
41 |
size_t small_page = os::vm_page_size(); |
|
42 |
if (large_page > small_page) { |
|
43 |
size_t num_small_in_large = large_page / small_page; |
|
44 |
size_t page = os::page_size_for_region_aligned(large_page, num_small_in_large); |
|
45 |
ASSERT_EQ(page, small_page) << "Did not get a small page"; |
|
46 |
} |
|
47 |
} |
|
48 |
||
42056 | 49 |
TEST_VM(os, page_size_for_region_aligned) { |
50 |
if (UseLargePages) { |
|
51 |
const size_t small_page = small_page_size(); |
|
52 |
const size_t large_page = large_page_size(); |
|
53 |
||
54 |
if (large_page > small_page) { |
|
55 |
size_t num_small_pages_in_large = large_page / small_page; |
|
56 |
size_t page = os::page_size_for_region_aligned(large_page, num_small_pages_in_large); |
|
57 |
||
58 |
ASSERT_EQ(page, small_page); |
|
59 |
} |
|
60 |
} |
|
61 |
} |
|
62 |
||
63 |
TEST_VM(os, page_size_for_region_alignment) { |
|
64 |
if (UseLargePages) { |
|
65 |
const size_t small_page = small_page_size(); |
|
66 |
const size_t large_page = large_page_size(); |
|
67 |
if (large_page > small_page) { |
|
68 |
const size_t unaligned_region = large_page + 17; |
|
69 |
size_t page = os::page_size_for_region_aligned(unaligned_region, 1); |
|
70 |
ASSERT_EQ(page, small_page); |
|
71 |
||
72 |
const size_t num_pages = 5; |
|
73 |
const size_t aligned_region = large_page * num_pages; |
|
74 |
page = os::page_size_for_region_aligned(aligned_region, num_pages); |
|
75 |
ASSERT_EQ(page, large_page); |
|
76 |
} |
|
77 |
} |
|
78 |
} |
|
79 |
||
80 |
TEST_VM(os, page_size_for_region_unaligned) { |
|
81 |
if (UseLargePages) { |
|
82 |
// Given exact page size, should return that page size. |
|
83 |
for (size_t i = 0; os::_page_sizes[i] != 0; i++) { |
|
84 |
size_t expected = os::_page_sizes[i]; |
|
85 |
size_t actual = os::page_size_for_region_unaligned(expected, 1); |
|
86 |
ASSERT_EQ(expected, actual); |
|
87 |
} |
|
88 |
||
89 |
// Given slightly larger size than a page size, return the page size. |
|
90 |
for (size_t i = 0; os::_page_sizes[i] != 0; i++) { |
|
91 |
size_t expected = os::_page_sizes[i]; |
|
92 |
size_t actual = os::page_size_for_region_unaligned(expected + 17, 1); |
|
93 |
ASSERT_EQ(expected, actual); |
|
94 |
} |
|
95 |
||
96 |
// Given a slightly smaller size than a page size, |
|
97 |
// return the next smaller page size. |
|
98 |
if (os::_page_sizes[1] > os::_page_sizes[0]) { |
|
99 |
size_t expected = os::_page_sizes[0]; |
|
100 |
size_t actual = os::page_size_for_region_unaligned(os::_page_sizes[1] - 17, 1); |
|
101 |
ASSERT_EQ(actual, expected); |
|
102 |
} |
|
103 |
||
104 |
// Return small page size for values less than a small page. |
|
105 |
size_t small_page = small_page_size(); |
|
106 |
size_t actual = os::page_size_for_region_unaligned(small_page - 17, 1); |
|
107 |
ASSERT_EQ(small_page, actual); |
|
108 |
} |
|
109 |
} |
|
110 |
||
46587
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
111 |
TEST(os, test_random) { |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
112 |
const double m = 2147483647; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
113 |
double mean = 0.0, variance = 0.0, t; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
114 |
const int reps = 10000; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
115 |
unsigned int seed = 1; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
116 |
|
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
117 |
// tty->print_cr("seed %ld for %ld repeats...", seed, reps); |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
118 |
os::init_random(seed); |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
119 |
int num; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
120 |
for (int k = 0; k < reps; k++) { |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
121 |
num = os::random(); |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
122 |
double u = (double)num / m; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
123 |
ASSERT_TRUE(u >= 0.0 && u <= 1.0) << "bad random number!"; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
124 |
|
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
125 |
// calculate mean and variance of the random sequence |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
126 |
mean += u; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
127 |
variance += (u*u); |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
128 |
} |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
129 |
mean /= reps; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
130 |
variance /= (reps - 1); |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
131 |
|
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
132 |
ASSERT_EQ(num, 1043618065) << "bad seed"; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
133 |
// tty->print_cr("mean of the 1st 10000 numbers: %f", mean); |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
134 |
int intmean = mean*100; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
135 |
ASSERT_EQ(intmean, 50); |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
136 |
// tty->print_cr("variance of the 1st 10000 numbers: %f", variance); |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
137 |
int intvariance = variance*100; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
138 |
ASSERT_EQ(intvariance, 33); |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
139 |
const double eps = 0.0001; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
140 |
t = fabsd(mean - 0.5018); |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
141 |
ASSERT_LT(t, eps) << "bad mean"; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
142 |
t = (variance - 0.3355) < 0.0 ? -(variance - 0.3355) : variance - 0.3355; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
143 |
ASSERT_LT(t, eps) << "bad variance"; |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
144 |
} |
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
145 |
|
6c97f34cb194
8182554: Code for os::random() assumes long is 32 bits
coleenp
parents:
42056
diff
changeset
|
146 |
|
38255 | 147 |
#ifdef ASSERT |
148 |
TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages, "sanity") { |
|
149 |
size_t region_size = 16 * os::vm_page_size(); |
|
150 |
os::page_size_for_region_aligned(region_size, 0); // should assert |
|
151 |
} |
|
152 |
#endif |