Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/hotspot/gtest/runtime/test_os.cpp
41144 views
1
/*
2
* Copyright (c) 2016, 2021, 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 "memory/allocation.hpp"
26
#include "memory/resourceArea.hpp"
27
#include "runtime/os.hpp"
28
#include "runtime/thread.hpp"
29
#include "utilities/globalDefinitions.hpp"
30
#include "utilities/macros.hpp"
31
#include "utilities/ostream.hpp"
32
#include "utilities/align.hpp"
33
#include "unittest.hpp"
34
35
static size_t small_page_size() {
36
return os::vm_page_size();
37
}
38
39
static size_t large_page_size() {
40
const size_t large_page_size_example = 4 * M;
41
return os::page_size_for_region_aligned(large_page_size_example, 1);
42
}
43
44
TEST_VM(os, page_size_for_region) {
45
size_t large_page_example = 4 * M;
46
size_t large_page = os::page_size_for_region_aligned(large_page_example, 1);
47
48
size_t small_page = os::vm_page_size();
49
if (large_page > small_page) {
50
size_t num_small_in_large = large_page / small_page;
51
size_t page = os::page_size_for_region_aligned(large_page, num_small_in_large);
52
ASSERT_EQ(page, small_page) << "Did not get a small page";
53
}
54
}
55
56
TEST_VM(os, page_size_for_region_aligned) {
57
if (UseLargePages) {
58
const size_t small_page = small_page_size();
59
const size_t large_page = large_page_size();
60
61
if (large_page > small_page) {
62
size_t num_small_pages_in_large = large_page / small_page;
63
size_t page = os::page_size_for_region_aligned(large_page, num_small_pages_in_large);
64
65
ASSERT_EQ(page, small_page);
66
}
67
}
68
}
69
70
TEST_VM(os, page_size_for_region_alignment) {
71
if (UseLargePages) {
72
const size_t small_page = small_page_size();
73
const size_t large_page = large_page_size();
74
if (large_page > small_page) {
75
const size_t unaligned_region = large_page + 17;
76
size_t page = os::page_size_for_region_aligned(unaligned_region, 1);
77
ASSERT_EQ(page, small_page);
78
79
const size_t num_pages = 5;
80
const size_t aligned_region = large_page * num_pages;
81
page = os::page_size_for_region_aligned(aligned_region, num_pages);
82
ASSERT_EQ(page, large_page);
83
}
84
}
85
}
86
87
TEST_VM(os, page_size_for_region_unaligned) {
88
if (UseLargePages) {
89
// Given exact page size, should return that page size.
90
for (size_t s = os::page_sizes().largest(); s != 0; s = os::page_sizes().next_smaller(s)) {
91
size_t actual = os::page_size_for_region_unaligned(s, 1);
92
ASSERT_EQ(s, actual);
93
}
94
95
// Given slightly larger size than a page size, return the page size.
96
for (size_t s = os::page_sizes().largest(); s != 0; s = os::page_sizes().next_smaller(s)) {
97
size_t actual = os::page_size_for_region_unaligned(s + 17, 1);
98
ASSERT_EQ(s, actual);
99
}
100
101
// Given a slightly smaller size than a page size,
102
// return the next smaller page size.
103
for (size_t s = os::page_sizes().largest(); s != 0; s = os::page_sizes().next_smaller(s)) {
104
const size_t expected = os::page_sizes().next_smaller(s);
105
if (expected != 0) {
106
size_t actual = os::page_size_for_region_unaligned(s - 17, 1);
107
ASSERT_EQ(actual, expected);
108
}
109
}
110
111
// Return small page size for values less than a small page.
112
size_t small_page = os::page_sizes().smallest();
113
size_t actual = os::page_size_for_region_unaligned(small_page - 17, 1);
114
ASSERT_EQ(small_page, actual);
115
}
116
}
117
118
TEST(os, test_random) {
119
const double m = 2147483647;
120
double mean = 0.0, variance = 0.0, t;
121
const int reps = 10000;
122
unsigned int seed = 1;
123
124
// tty->print_cr("seed %ld for %ld repeats...", seed, reps);
125
int num;
126
for (int k = 0; k < reps; k++) {
127
// Use next_random so the calculation is stateless.
128
num = seed = os::next_random(seed);
129
double u = (double)num / m;
130
ASSERT_TRUE(u >= 0.0 && u <= 1.0) << "bad random number!";
131
132
// calculate mean and variance of the random sequence
133
mean += u;
134
variance += (u*u);
135
}
136
mean /= reps;
137
variance /= (reps - 1);
138
139
ASSERT_EQ(num, 1043618065) << "bad seed";
140
// tty->print_cr("mean of the 1st 10000 numbers: %f", mean);
141
int intmean = mean*100;
142
ASSERT_EQ(intmean, 50);
143
// tty->print_cr("variance of the 1st 10000 numbers: %f", variance);
144
int intvariance = variance*100;
145
ASSERT_EQ(intvariance, 33);
146
const double eps = 0.0001;
147
t = fabsd(mean - 0.5018);
148
ASSERT_LT(t, eps) << "bad mean";
149
t = (variance - 0.3355) < 0.0 ? -(variance - 0.3355) : variance - 0.3355;
150
ASSERT_LT(t, eps) << "bad variance";
151
}
152
153
#ifdef ASSERT
154
TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages,
155
"assert.min_pages > 0. failed: sanity") {
156
size_t region_size = 16 * os::vm_page_size();
157
os::page_size_for_region_aligned(region_size, 0); // should assert
158
}
159
#endif
160
161
static void do_test_print_hex_dump(address addr, size_t len, int unitsize, const char* expected) {
162
char buf[256];
163
buf[0] = '\0';
164
stringStream ss(buf, sizeof(buf));
165
os::print_hex_dump(&ss, addr, addr + len, unitsize);
166
// tty->print_cr("expected: %s", expected);
167
// tty->print_cr("result: %s", buf);
168
ASSERT_NE(strstr(buf, expected), (char*)NULL);
169
}
170
171
TEST_VM(os, test_print_hex_dump) {
172
const char* pattern [4] = {
173
#ifdef VM_LITTLE_ENDIAN
174
"00 01 02 03 04 05 06 07",
175
"0100 0302 0504 0706",
176
"03020100 07060504",
177
"0706050403020100"
178
#else
179
"00 01 02 03 04 05 06 07",
180
"0001 0203 0405 0607",
181
"00010203 04050607",
182
"0001020304050607"
183
#endif
184
};
185
186
const char* pattern_not_readable [4] = {
187
"?? ?? ?? ?? ?? ?? ?? ??",
188
"???? ???? ???? ????",
189
"???????? ????????",
190
"????????????????"
191
};
192
193
// On AIX, zero page is readable.
194
address unreadable =
195
#ifdef AIX
196
(address) 0xFFFFFFFFFFFF0000ULL;
197
#else
198
(address) 0
199
#endif
200
;
201
202
ResourceMark rm;
203
char buf[64];
204
stringStream ss(buf, sizeof(buf));
205
outputStream* out = &ss;
206
// outputStream* out = tty; // enable for printout
207
208
// Test dumping unreadable memory
209
// Exclude test for Windows for now, since it needs SEH handling to work which cannot be
210
// guaranteed when we call directly into VM code. (see JDK-8220220)
211
#ifndef _WIN32
212
do_test_print_hex_dump(unreadable, 100, 1, pattern_not_readable[0]);
213
do_test_print_hex_dump(unreadable, 100, 2, pattern_not_readable[1]);
214
do_test_print_hex_dump(unreadable, 100, 4, pattern_not_readable[2]);
215
do_test_print_hex_dump(unreadable, 100, 8, pattern_not_readable[3]);
216
#endif
217
218
// Test dumping readable memory
219
address arr = (address)os::malloc(100, mtInternal);
220
for (int c = 0; c < 100; c++) {
221
arr[c] = c;
222
}
223
224
// properly aligned
225
do_test_print_hex_dump(arr, 100, 1, pattern[0]);
226
do_test_print_hex_dump(arr, 100, 2, pattern[1]);
227
do_test_print_hex_dump(arr, 100, 4, pattern[2]);
228
do_test_print_hex_dump(arr, 100, 8, pattern[3]);
229
230
// Not properly aligned. Should automatically down-align by unitsize
231
do_test_print_hex_dump(arr + 1, 100, 2, pattern[1]);
232
do_test_print_hex_dump(arr + 1, 100, 4, pattern[2]);
233
do_test_print_hex_dump(arr + 1, 100, 8, pattern[3]);
234
235
os::free(arr);
236
}
237
238
//////////////////////////////////////////////////////////////////////////////
239
// Test os::vsnprintf and friends.
240
241
static void check_snprintf_result(int expected, size_t limit, int actual, bool expect_count) {
242
if (expect_count || ((size_t)expected < limit)) {
243
ASSERT_EQ(expected, actual);
244
} else {
245
ASSERT_GT(0, actual);
246
}
247
}
248
249
// PrintFn is expected to be int (*)(char*, size_t, const char*, ...).
250
// But jio_snprintf is a C-linkage function with that signature, which
251
// has a different type on some platforms (like Solaris).
252
template<typename PrintFn>
253
static void test_snprintf(PrintFn pf, bool expect_count) {
254
const char expected[] = "abcdefghijklmnopqrstuvwxyz";
255
const int expected_len = sizeof(expected) - 1;
256
const size_t padding_size = 10;
257
char buffer[2 * (sizeof(expected) + padding_size)];
258
char check_buffer[sizeof(buffer)];
259
const char check_char = '1'; // Something not in expected.
260
memset(check_buffer, check_char, sizeof(check_buffer));
261
const size_t sizes_to_test[] = {
262
sizeof(buffer) - padding_size, // Fits, with plenty of space to spare.
263
sizeof(buffer)/2, // Fits, with space to spare.
264
sizeof(buffer)/4, // Doesn't fit.
265
sizeof(expected) + padding_size + 1, // Fits, with a little room to spare
266
sizeof(expected) + padding_size, // Fits exactly.
267
sizeof(expected) + padding_size - 1, // Doesn't quite fit.
268
2, // One char + terminating NUL.
269
1, // Only space for terminating NUL.
270
0 }; // No space at all.
271
for (unsigned i = 0; i < ARRAY_SIZE(sizes_to_test); ++i) {
272
memset(buffer, check_char, sizeof(buffer)); // To catch stray writes.
273
size_t test_size = sizes_to_test[i];
274
ResourceMark rm;
275
stringStream s;
276
s.print("test_size: " SIZE_FORMAT, test_size);
277
SCOPED_TRACE(s.as_string());
278
size_t prefix_size = padding_size;
279
guarantee(test_size <= (sizeof(buffer) - prefix_size), "invariant");
280
size_t write_size = MIN2(sizeof(expected), test_size);
281
size_t suffix_size = sizeof(buffer) - prefix_size - write_size;
282
char* write_start = buffer + prefix_size;
283
char* write_end = write_start + write_size;
284
285
int result = pf(write_start, test_size, "%s", expected);
286
287
check_snprintf_result(expected_len, test_size, result, expect_count);
288
289
// Verify expected output.
290
if (test_size > 0) {
291
ASSERT_EQ(0, strncmp(write_start, expected, write_size - 1));
292
// Verify terminating NUL of output.
293
ASSERT_EQ('\0', write_start[write_size - 1]);
294
} else {
295
guarantee(test_size == 0, "invariant");
296
guarantee(write_size == 0, "invariant");
297
guarantee(prefix_size + suffix_size == sizeof(buffer), "invariant");
298
guarantee(write_start == write_end, "invariant");
299
}
300
301
// Verify no scribbling on prefix or suffix.
302
ASSERT_EQ(0, strncmp(buffer, check_buffer, prefix_size));
303
ASSERT_EQ(0, strncmp(write_end, check_buffer, suffix_size));
304
}
305
306
// Special case of 0-length buffer with empty (except for terminator) output.
307
check_snprintf_result(0, 0, pf(NULL, 0, "%s", ""), expect_count);
308
check_snprintf_result(0, 0, pf(NULL, 0, ""), expect_count);
309
}
310
311
// This is probably equivalent to os::snprintf, but we're being
312
// explicit about what we're testing here.
313
static int vsnprintf_wrapper(char* buf, size_t len, const char* fmt, ...) {
314
va_list args;
315
va_start(args, fmt);
316
int result = os::vsnprintf(buf, len, fmt, args);
317
va_end(args);
318
return result;
319
}
320
321
TEST_VM(os, vsnprintf) {
322
test_snprintf(vsnprintf_wrapper, true);
323
}
324
325
TEST_VM(os, snprintf) {
326
test_snprintf(os::snprintf, true);
327
}
328
329
// These are declared in jvm.h; test here, with related functions.
330
extern "C" {
331
int jio_vsnprintf(char*, size_t, const char*, va_list);
332
int jio_snprintf(char*, size_t, const char*, ...);
333
}
334
335
// This is probably equivalent to jio_snprintf, but we're being
336
// explicit about what we're testing here.
337
static int jio_vsnprintf_wrapper(char* buf, size_t len, const char* fmt, ...) {
338
va_list args;
339
va_start(args, fmt);
340
int result = jio_vsnprintf(buf, len, fmt, args);
341
va_end(args);
342
return result;
343
}
344
345
TEST_VM(os, jio_vsnprintf) {
346
test_snprintf(jio_vsnprintf_wrapper, false);
347
}
348
349
TEST_VM(os, jio_snprintf) {
350
test_snprintf(jio_snprintf, false);
351
}
352
353
// Test that os::release_memory() can deal with areas containing multiple mappings.
354
#define PRINT_MAPPINGS(s) { tty->print_cr("%s", s); os::print_memory_mappings((char*)p, total_range_len, tty); }
355
//#define PRINT_MAPPINGS
356
357
#ifndef _AIX // JDK-8257041
358
// Reserve an area consisting of multiple mappings
359
// (from multiple calls to os::reserve_memory)
360
static address reserve_multiple(int num_stripes, size_t stripe_len) {
361
assert(is_aligned(stripe_len, os::vm_allocation_granularity()), "Sanity");
362
size_t total_range_len = num_stripes * stripe_len;
363
// Reserve a large contiguous area to get the address space...
364
address p = (address)os::reserve_memory(total_range_len);
365
EXPECT_NE(p, (address)NULL);
366
// .. release it...
367
EXPECT_TRUE(os::release_memory((char*)p, total_range_len));
368
// ... re-reserve in the same spot multiple areas...
369
for (int stripe = 0; stripe < num_stripes; stripe++) {
370
address q = p + (stripe * stripe_len);
371
// Commit, alternatingly with or without exec permission,
372
// to prevent kernel from folding these mappings.
373
const bool executable = stripe % 2 == 0;
374
q = (address)os::attempt_reserve_memory_at((char*)q, stripe_len, executable);
375
EXPECT_NE(q, (address)NULL);
376
EXPECT_TRUE(os::commit_memory((char*)q, stripe_len, executable));
377
}
378
return p;
379
}
380
#endif // !AIX
381
382
// Reserve an area with a single call to os::reserve_memory,
383
// with multiple committed and uncommitted regions
384
static address reserve_one_commit_multiple(int num_stripes, size_t stripe_len) {
385
assert(is_aligned(stripe_len, os::vm_allocation_granularity()), "Sanity");
386
size_t total_range_len = num_stripes * stripe_len;
387
address p = (address)os::reserve_memory(total_range_len);
388
EXPECT_NE(p, (address)NULL);
389
for (int stripe = 0; stripe < num_stripes; stripe++) {
390
address q = p + (stripe * stripe_len);
391
if (stripe % 2 == 0) {
392
EXPECT_TRUE(os::commit_memory((char*)q, stripe_len, false));
393
}
394
}
395
return p;
396
}
397
398
#ifdef _WIN32
399
// Release a range allocated with reserve_multiple carefully, to not trip mapping
400
// asserts on Windows in os::release_memory()
401
static void carefully_release_multiple(address start, int num_stripes, size_t stripe_len) {
402
for (int stripe = 0; stripe < num_stripes; stripe++) {
403
address q = start + (stripe * stripe_len);
404
EXPECT_TRUE(os::release_memory((char*)q, stripe_len));
405
}
406
}
407
struct NUMASwitcher {
408
const bool _b;
409
NUMASwitcher(bool v): _b(UseNUMAInterleaving) { UseNUMAInterleaving = v; }
410
~NUMASwitcher() { UseNUMAInterleaving = _b; }
411
};
412
#endif
413
414
#ifndef _AIX // JDK-8257041
415
#if defined(__APPLE__) && !defined(AARCH64) // JDK-8267339
416
TEST_VM(os, DISABLED_release_multi_mappings) {
417
#else
418
TEST_VM(os, release_multi_mappings) {
419
#endif
420
// Test that we can release an area created with multiple reservation calls
421
const size_t stripe_len = 4 * M;
422
const int num_stripes = 4;
423
const size_t total_range_len = stripe_len * num_stripes;
424
425
// reserve address space...
426
address p = reserve_multiple(num_stripes, stripe_len);
427
ASSERT_NE(p, (address)NULL);
428
PRINT_MAPPINGS("A");
429
430
// .. release it...
431
{
432
// On Windows, use UseNUMAInterleaving=1 which makes
433
// os::release_memory accept multi-map-ranges.
434
// Otherwise we would assert (see below for death test).
435
WINDOWS_ONLY(NUMASwitcher b(true);)
436
ASSERT_TRUE(os::release_memory((char*)p, total_range_len));
437
}
438
PRINT_MAPPINGS("B");
439
440
// re-reserve it. This should work unless release failed.
441
address p2 = (address)os::attempt_reserve_memory_at((char*)p, total_range_len);
442
ASSERT_EQ(p2, p);
443
PRINT_MAPPINGS("C");
444
445
ASSERT_TRUE(os::release_memory((char*)p, total_range_len));
446
}
447
#endif // !AIX
448
449
#ifdef _WIN32
450
// On Windows, test that we recognize bad ranges.
451
// On debug this would assert. Test that too.
452
// On other platforms, we are unable to recognize bad ranges.
453
#ifdef ASSERT
454
TEST_VM_ASSERT_MSG(os, release_bad_ranges, ".*bad release") {
455
#else
456
TEST_VM(os, release_bad_ranges) {
457
#endif
458
char* p = os::reserve_memory(4 * M);
459
ASSERT_NE(p, (char*)NULL);
460
// Release part of range
461
ASSERT_FALSE(os::release_memory(p, M));
462
// Release part of range
463
ASSERT_FALSE(os::release_memory(p + M, M));
464
// Release more than the range (explicitly switch off NUMA here
465
// to make os::release_memory() test more strictly and to not
466
// accidentally release neighbors)
467
{
468
NUMASwitcher b(false);
469
ASSERT_FALSE(os::release_memory(p, M * 5));
470
ASSERT_FALSE(os::release_memory(p - M, M * 5));
471
ASSERT_FALSE(os::release_memory(p - M, M * 6));
472
}
473
474
ASSERT_TRUE(os::release_memory(p, 4 * M)); // Release for real
475
ASSERT_FALSE(os::release_memory(p, 4 * M)); // Again, should fail
476
}
477
#endif // _WIN32
478
479
TEST_VM(os, release_one_mapping_multi_commits) {
480
// Test that we can release an area consisting of interleaved
481
// committed and uncommitted regions:
482
const size_t stripe_len = 4 * M;
483
const int num_stripes = 4;
484
const size_t total_range_len = stripe_len * num_stripes;
485
486
// reserve address space...
487
address p = reserve_one_commit_multiple(num_stripes, stripe_len);
488
ASSERT_NE(p, (address)NULL);
489
PRINT_MAPPINGS("A");
490
491
// .. release it...
492
ASSERT_TRUE(os::release_memory((char*)p, total_range_len));
493
PRINT_MAPPINGS("B");
494
495
// re-reserve it. This should work unless release failed.
496
address p2 = (address)os::attempt_reserve_memory_at((char*)p, total_range_len);
497
ASSERT_EQ(p2, p);
498
PRINT_MAPPINGS("C");
499
500
ASSERT_TRUE(os::release_memory((char*)p, total_range_len));
501
PRINT_MAPPINGS("D");
502
}
503
504
static void test_show_mappings(address start, size_t size) {
505
// Note: should this overflow, thats okay. stream will silently truncate. Does not matter for the test.
506
const size_t buflen = 4 * M;
507
char* buf = NEW_C_HEAP_ARRAY(char, buflen, mtInternal);
508
buf[0] = '\0';
509
stringStream ss(buf, buflen);
510
if (start != nullptr) {
511
os::print_memory_mappings((char*)start, size, &ss);
512
} else {
513
os::print_memory_mappings(&ss); // prints full address space
514
}
515
// Still an empty implementation on MacOS and AIX
516
#if defined(LINUX) || defined(_WIN32)
517
EXPECT_NE(buf[0], '\0');
518
#endif
519
// buf[buflen - 1] = '\0';
520
// tty->print_raw(buf);
521
FREE_C_HEAP_ARRAY(char, buf);
522
}
523
524
TEST_VM(os, show_mappings_small_range) {
525
test_show_mappings((address)0x100000, 2 * G);
526
}
527
528
TEST_VM(os, show_mappings_full_range) {
529
// Reserve a small range and fill it with a marker string, should show up
530
// on implementations displaying range snippets
531
char* p = os::reserve_memory(1 * M, false, mtInternal);
532
if (p != nullptr) {
533
if (os::commit_memory(p, 1 * M, false)) {
534
strcpy(p, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
535
}
536
}
537
test_show_mappings(nullptr, 0);
538
if (p != nullptr) {
539
os::release_memory(p, 1 * M);
540
}
541
}
542
543
#ifdef _WIN32
544
// Test os::win32::find_mapping
545
TEST_VM(os, find_mapping_simple) {
546
const size_t total_range_len = 4 * M;
547
os::win32::mapping_info_t mapping_info;
548
549
// Some obvious negatives
550
ASSERT_FALSE(os::win32::find_mapping((address)NULL, &mapping_info));
551
ASSERT_FALSE(os::win32::find_mapping((address)4711, &mapping_info));
552
553
// A simple allocation
554
{
555
address p = (address)os::reserve_memory(total_range_len);
556
ASSERT_NE(p, (address)NULL);
557
PRINT_MAPPINGS("A");
558
for (size_t offset = 0; offset < total_range_len; offset += 4711) {
559
ASSERT_TRUE(os::win32::find_mapping(p + offset, &mapping_info));
560
ASSERT_EQ(mapping_info.base, p);
561
ASSERT_EQ(mapping_info.regions, 1);
562
ASSERT_EQ(mapping_info.size, total_range_len);
563
ASSERT_EQ(mapping_info.committed_size, 0);
564
}
565
// Test just outside the allocation
566
if (os::win32::find_mapping(p - 1, &mapping_info)) {
567
ASSERT_NE(mapping_info.base, p);
568
}
569
if (os::win32::find_mapping(p + total_range_len, &mapping_info)) {
570
ASSERT_NE(mapping_info.base, p);
571
}
572
ASSERT_TRUE(os::release_memory((char*)p, total_range_len));
573
PRINT_MAPPINGS("B");
574
ASSERT_FALSE(os::win32::find_mapping(p, &mapping_info));
575
}
576
}
577
578
TEST_VM(os, find_mapping_2) {
579
// A more complex allocation, consisting of multiple regions.
580
const size_t total_range_len = 4 * M;
581
os::win32::mapping_info_t mapping_info;
582
583
const size_t stripe_len = total_range_len / 4;
584
address p = reserve_one_commit_multiple(4, stripe_len);
585
ASSERT_NE(p, (address)NULL);
586
PRINT_MAPPINGS("A");
587
for (size_t offset = 0; offset < total_range_len; offset += 4711) {
588
ASSERT_TRUE(os::win32::find_mapping(p + offset, &mapping_info));
589
ASSERT_EQ(mapping_info.base, p);
590
ASSERT_EQ(mapping_info.regions, 4);
591
ASSERT_EQ(mapping_info.size, total_range_len);
592
ASSERT_EQ(mapping_info.committed_size, total_range_len / 2);
593
}
594
// Test just outside the allocation
595
if (os::win32::find_mapping(p - 1, &mapping_info)) {
596
ASSERT_NE(mapping_info.base, p);
597
}
598
if (os::win32::find_mapping(p + total_range_len, &mapping_info)) {
599
ASSERT_NE(mapping_info.base, p);
600
}
601
ASSERT_TRUE(os::release_memory((char*)p, total_range_len));
602
PRINT_MAPPINGS("B");
603
ASSERT_FALSE(os::win32::find_mapping(p, &mapping_info));
604
}
605
606
TEST_VM(os, find_mapping_3) {
607
const size_t total_range_len = 4 * M;
608
os::win32::mapping_info_t mapping_info;
609
610
// A more complex case, consisting of multiple allocations.
611
{
612
const size_t stripe_len = total_range_len / 4;
613
address p = reserve_multiple(4, stripe_len);
614
ASSERT_NE(p, (address)NULL);
615
PRINT_MAPPINGS("E");
616
for (int stripe = 0; stripe < 4; stripe++) {
617
ASSERT_TRUE(os::win32::find_mapping(p + (stripe * stripe_len), &mapping_info));
618
ASSERT_EQ(mapping_info.base, p + (stripe * stripe_len));
619
ASSERT_EQ(mapping_info.regions, 1);
620
ASSERT_EQ(mapping_info.size, stripe_len);
621
ASSERT_EQ(mapping_info.committed_size, stripe_len);
622
}
623
carefully_release_multiple(p, 4, stripe_len);
624
PRINT_MAPPINGS("F");
625
ASSERT_FALSE(os::win32::find_mapping(p, &mapping_info));
626
}
627
}
628
#endif // _WIN32
629
630
TEST_VM(os, os_pagesizes) {
631
ASSERT_EQ(os::min_page_size(), 4 * K);
632
ASSERT_LE(os::min_page_size(), (size_t)os::vm_page_size());
633
// The vm_page_size should be the smallest in the set of allowed page sizes
634
// (contract says "default" page size but a lot of code actually assumes
635
// this to be the smallest page size; notable, deliberate exception is
636
// AIX which can have smaller page sizes but those are not part of the
637
// page_sizes() set).
638
ASSERT_EQ(os::page_sizes().smallest(), (size_t)os::vm_page_size());
639
// The large page size, if it exists, shall be part of the set
640
if (UseLargePages) {
641
ASSERT_GT(os::large_page_size(), (size_t)os::vm_page_size());
642
ASSERT_TRUE(os::page_sizes().contains(os::large_page_size()));
643
}
644
os::page_sizes().print_on(tty);
645
tty->cr();
646
}
647
648
static const int min_page_size_log2 = exact_log2(os::min_page_size());
649
static const int max_page_size_log2 = (int)BitsPerWord;
650
651
TEST_VM(os, pagesizes_test_range) {
652
for (int bit = min_page_size_log2; bit < max_page_size_log2; bit++) {
653
for (int bit2 = min_page_size_log2; bit2 < max_page_size_log2; bit2++) {
654
const size_t s = (size_t)1 << bit;
655
const size_t s2 = (size_t)1 << bit2;
656
os::PageSizes pss;
657
ASSERT_EQ((size_t)0, pss.smallest());
658
ASSERT_EQ((size_t)0, pss.largest());
659
// one size set
660
pss.add(s);
661
ASSERT_TRUE(pss.contains(s));
662
ASSERT_EQ(s, pss.smallest());
663
ASSERT_EQ(s, pss.largest());
664
ASSERT_EQ(pss.next_larger(s), (size_t)0);
665
ASSERT_EQ(pss.next_smaller(s), (size_t)0);
666
// two set
667
pss.add(s2);
668
ASSERT_TRUE(pss.contains(s2));
669
if (s2 < s) {
670
ASSERT_EQ(s2, pss.smallest());
671
ASSERT_EQ(s, pss.largest());
672
ASSERT_EQ(pss.next_larger(s2), (size_t)s);
673
ASSERT_EQ(pss.next_smaller(s2), (size_t)0);
674
ASSERT_EQ(pss.next_larger(s), (size_t)0);
675
ASSERT_EQ(pss.next_smaller(s), (size_t)s2);
676
} else if (s2 > s) {
677
ASSERT_EQ(s, pss.smallest());
678
ASSERT_EQ(s2, pss.largest());
679
ASSERT_EQ(pss.next_larger(s), (size_t)s2);
680
ASSERT_EQ(pss.next_smaller(s), (size_t)0);
681
ASSERT_EQ(pss.next_larger(s2), (size_t)0);
682
ASSERT_EQ(pss.next_smaller(s2), (size_t)s);
683
}
684
for (int bit3 = min_page_size_log2; bit3 < max_page_size_log2; bit3++) {
685
const size_t s3 = (size_t)1 << bit3;
686
ASSERT_EQ(s3 == s || s3 == s2, pss.contains(s3));
687
}
688
}
689
}
690
}
691
692
TEST_VM(os, pagesizes_test_print) {
693
os::PageSizes pss;
694
const size_t sizes[] = { 16 * K, 64 * K, 128 * K, 1 * M, 4 * M, 1 * G, 2 * G, 0 };
695
static const char* const expected = "16k, 64k, 128k, 1M, 4M, 1G, 2G";
696
for (int i = 0; sizes[i] != 0; i++) {
697
pss.add(sizes[i]);
698
}
699
char buffer[256];
700
stringStream ss(buffer, sizeof(buffer));
701
pss.print_on(&ss);
702
ASSERT_EQ(strcmp(expected, buffer), 0);
703
}
704
705
TEST_VM(os, dll_address_to_function_and_library_name) {
706
char tmp[1024];
707
char output[1024];
708
stringStream st(output, sizeof(output));
709
710
#define EXPECT_CONTAINS(haystack, needle) \
711
EXPECT_NE(::strstr(haystack, needle), (char*)NULL)
712
#define EXPECT_DOES_NOT_CONTAIN(haystack, needle) \
713
EXPECT_EQ(::strstr(haystack, needle), (char*)NULL)
714
// #define LOG(...) tty->print_cr(__VA_ARGS__); // enable if needed
715
#define LOG(...)
716
717
// Invalid addresses
718
address addr = (address)(intptr_t)-1;
719
EXPECT_FALSE(os::print_function_and_library_name(&st, addr));
720
addr = NULL;
721
EXPECT_FALSE(os::print_function_and_library_name(&st, addr));
722
723
// Valid addresses
724
// Test with or without shorten-paths, demangle, and scratch buffer
725
for (int i = 0; i < 16; i++) {
726
const bool shorten_paths = (i & 1) != 0;
727
const bool demangle = (i & 2) != 0;
728
const bool strip_arguments = (i & 4) != 0;
729
const bool provide_scratch_buffer = (i & 8) != 0;
730
LOG("shorten_paths=%d, demangle=%d, strip_arguments=%d, provide_scratch_buffer=%d",
731
shorten_paths, demangle, strip_arguments, provide_scratch_buffer);
732
733
// Should show os::min_page_size in libjvm
734
addr = CAST_FROM_FN_PTR(address, Threads::create_vm);
735
st.reset();
736
EXPECT_TRUE(os::print_function_and_library_name(&st, addr,
737
provide_scratch_buffer ? tmp : NULL,
738
sizeof(tmp),
739
shorten_paths, demangle,
740
strip_arguments));
741
EXPECT_CONTAINS(output, "Threads");
742
EXPECT_CONTAINS(output, "create_vm");
743
EXPECT_CONTAINS(output, "jvm"); // "jvm.dll" or "libjvm.so" or similar
744
LOG("%s", output);
745
746
// Test truncation on scratch buffer
747
if (provide_scratch_buffer) {
748
st.reset();
749
tmp[10] = 'X';
750
EXPECT_TRUE(os::print_function_and_library_name(&st, addr, tmp, 10,
751
shorten_paths, demangle));
752
EXPECT_EQ(tmp[10], 'X');
753
LOG("%s", output);
754
}
755
}
756
}
757
758
// Not a regex! Very primitive, just match:
759
// "d" - digit
760
// "a" - ascii
761
// "." - everything
762
// rest must match
763
static bool very_simple_string_matcher(const char* pattern, const char* s) {
764
const size_t lp = strlen(pattern);
765
const size_t ls = strlen(s);
766
if (ls < lp) {
767
return false;
768
}
769
for (size_t i = 0; i < lp; i ++) {
770
switch (pattern[i]) {
771
case '.': continue;
772
case 'd': if (!isdigit(s[i])) return false; break;
773
case 'a': if (!isascii(s[i])) return false; break;
774
default: if (s[i] != pattern[i]) return false; break;
775
}
776
}
777
return true;
778
}
779
780
TEST_VM(os, iso8601_time) {
781
char buffer[os::iso8601_timestamp_size + 1]; // + space for canary
782
buffer[os::iso8601_timestamp_size] = 'X'; // canary
783
const char* result = NULL;
784
// YYYY-MM-DDThh:mm:ss.mmm+zzzz
785
const char* const pattern_utc = "dddd-dd-dd.dd:dd:dd.ddd.0000";
786
const char* const pattern_local = "dddd-dd-dd.dd:dd:dd.ddd.dddd";
787
788
result = os::iso8601_time(buffer, sizeof(buffer), true);
789
tty->print_cr("%s", result);
790
EXPECT_EQ(result, buffer);
791
EXPECT_TRUE(very_simple_string_matcher(pattern_utc, result));
792
793
result = os::iso8601_time(buffer, sizeof(buffer), false);
794
tty->print_cr("%s", result);
795
EXPECT_EQ(result, buffer);
796
EXPECT_TRUE(very_simple_string_matcher(pattern_local, result));
797
798
// Test with explicit timestamps
799
result = os::iso8601_time(0, buffer, sizeof(buffer), true);
800
tty->print_cr("%s", result);
801
EXPECT_EQ(result, buffer);
802
EXPECT_TRUE(very_simple_string_matcher("1970-01-01.00:00:00.000+0000", result));
803
804
result = os::iso8601_time(17, buffer, sizeof(buffer), true);
805
tty->print_cr("%s", result);
806
EXPECT_EQ(result, buffer);
807
EXPECT_TRUE(very_simple_string_matcher("1970-01-01.00:00:00.017+0000", result));
808
809
// Canary should still be intact
810
EXPECT_EQ(buffer[os::iso8601_timestamp_size], 'X');
811
}
812
813