Path: blob/master/test/hotspot/gtest/runtime/test_os.cpp
41144 views
/*1* Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223#include "precompiled.hpp"24#include "memory/allocation.hpp"25#include "memory/resourceArea.hpp"26#include "runtime/os.hpp"27#include "runtime/thread.hpp"28#include "utilities/globalDefinitions.hpp"29#include "utilities/macros.hpp"30#include "utilities/ostream.hpp"31#include "utilities/align.hpp"32#include "unittest.hpp"3334static size_t small_page_size() {35return os::vm_page_size();36}3738static size_t large_page_size() {39const size_t large_page_size_example = 4 * M;40return os::page_size_for_region_aligned(large_page_size_example, 1);41}4243TEST_VM(os, page_size_for_region) {44size_t large_page_example = 4 * M;45size_t large_page = os::page_size_for_region_aligned(large_page_example, 1);4647size_t small_page = os::vm_page_size();48if (large_page > small_page) {49size_t num_small_in_large = large_page / small_page;50size_t page = os::page_size_for_region_aligned(large_page, num_small_in_large);51ASSERT_EQ(page, small_page) << "Did not get a small page";52}53}5455TEST_VM(os, page_size_for_region_aligned) {56if (UseLargePages) {57const size_t small_page = small_page_size();58const size_t large_page = large_page_size();5960if (large_page > small_page) {61size_t num_small_pages_in_large = large_page / small_page;62size_t page = os::page_size_for_region_aligned(large_page, num_small_pages_in_large);6364ASSERT_EQ(page, small_page);65}66}67}6869TEST_VM(os, page_size_for_region_alignment) {70if (UseLargePages) {71const size_t small_page = small_page_size();72const size_t large_page = large_page_size();73if (large_page > small_page) {74const size_t unaligned_region = large_page + 17;75size_t page = os::page_size_for_region_aligned(unaligned_region, 1);76ASSERT_EQ(page, small_page);7778const size_t num_pages = 5;79const size_t aligned_region = large_page * num_pages;80page = os::page_size_for_region_aligned(aligned_region, num_pages);81ASSERT_EQ(page, large_page);82}83}84}8586TEST_VM(os, page_size_for_region_unaligned) {87if (UseLargePages) {88// Given exact page size, should return that page size.89for (size_t s = os::page_sizes().largest(); s != 0; s = os::page_sizes().next_smaller(s)) {90size_t actual = os::page_size_for_region_unaligned(s, 1);91ASSERT_EQ(s, actual);92}9394// Given slightly larger size than a page size, return the page size.95for (size_t s = os::page_sizes().largest(); s != 0; s = os::page_sizes().next_smaller(s)) {96size_t actual = os::page_size_for_region_unaligned(s + 17, 1);97ASSERT_EQ(s, actual);98}99100// Given a slightly smaller size than a page size,101// return the next smaller page size.102for (size_t s = os::page_sizes().largest(); s != 0; s = os::page_sizes().next_smaller(s)) {103const size_t expected = os::page_sizes().next_smaller(s);104if (expected != 0) {105size_t actual = os::page_size_for_region_unaligned(s - 17, 1);106ASSERT_EQ(actual, expected);107}108}109110// Return small page size for values less than a small page.111size_t small_page = os::page_sizes().smallest();112size_t actual = os::page_size_for_region_unaligned(small_page - 17, 1);113ASSERT_EQ(small_page, actual);114}115}116117TEST(os, test_random) {118const double m = 2147483647;119double mean = 0.0, variance = 0.0, t;120const int reps = 10000;121unsigned int seed = 1;122123// tty->print_cr("seed %ld for %ld repeats...", seed, reps);124int num;125for (int k = 0; k < reps; k++) {126// Use next_random so the calculation is stateless.127num = seed = os::next_random(seed);128double u = (double)num / m;129ASSERT_TRUE(u >= 0.0 && u <= 1.0) << "bad random number!";130131// calculate mean and variance of the random sequence132mean += u;133variance += (u*u);134}135mean /= reps;136variance /= (reps - 1);137138ASSERT_EQ(num, 1043618065) << "bad seed";139// tty->print_cr("mean of the 1st 10000 numbers: %f", mean);140int intmean = mean*100;141ASSERT_EQ(intmean, 50);142// tty->print_cr("variance of the 1st 10000 numbers: %f", variance);143int intvariance = variance*100;144ASSERT_EQ(intvariance, 33);145const double eps = 0.0001;146t = fabsd(mean - 0.5018);147ASSERT_LT(t, eps) << "bad mean";148t = (variance - 0.3355) < 0.0 ? -(variance - 0.3355) : variance - 0.3355;149ASSERT_LT(t, eps) << "bad variance";150}151152#ifdef ASSERT153TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages,154"assert.min_pages > 0. failed: sanity") {155size_t region_size = 16 * os::vm_page_size();156os::page_size_for_region_aligned(region_size, 0); // should assert157}158#endif159160static void do_test_print_hex_dump(address addr, size_t len, int unitsize, const char* expected) {161char buf[256];162buf[0] = '\0';163stringStream ss(buf, sizeof(buf));164os::print_hex_dump(&ss, addr, addr + len, unitsize);165// tty->print_cr("expected: %s", expected);166// tty->print_cr("result: %s", buf);167ASSERT_NE(strstr(buf, expected), (char*)NULL);168}169170TEST_VM(os, test_print_hex_dump) {171const char* pattern [4] = {172#ifdef VM_LITTLE_ENDIAN173"00 01 02 03 04 05 06 07",174"0100 0302 0504 0706",175"03020100 07060504",176"0706050403020100"177#else178"00 01 02 03 04 05 06 07",179"0001 0203 0405 0607",180"00010203 04050607",181"0001020304050607"182#endif183};184185const char* pattern_not_readable [4] = {186"?? ?? ?? ?? ?? ?? ?? ??",187"???? ???? ???? ????",188"???????? ????????",189"????????????????"190};191192// On AIX, zero page is readable.193address unreadable =194#ifdef AIX195(address) 0xFFFFFFFFFFFF0000ULL;196#else197(address) 0198#endif199;200201ResourceMark rm;202char buf[64];203stringStream ss(buf, sizeof(buf));204outputStream* out = &ss;205// outputStream* out = tty; // enable for printout206207// Test dumping unreadable memory208// Exclude test for Windows for now, since it needs SEH handling to work which cannot be209// guaranteed when we call directly into VM code. (see JDK-8220220)210#ifndef _WIN32211do_test_print_hex_dump(unreadable, 100, 1, pattern_not_readable[0]);212do_test_print_hex_dump(unreadable, 100, 2, pattern_not_readable[1]);213do_test_print_hex_dump(unreadable, 100, 4, pattern_not_readable[2]);214do_test_print_hex_dump(unreadable, 100, 8, pattern_not_readable[3]);215#endif216217// Test dumping readable memory218address arr = (address)os::malloc(100, mtInternal);219for (int c = 0; c < 100; c++) {220arr[c] = c;221}222223// properly aligned224do_test_print_hex_dump(arr, 100, 1, pattern[0]);225do_test_print_hex_dump(arr, 100, 2, pattern[1]);226do_test_print_hex_dump(arr, 100, 4, pattern[2]);227do_test_print_hex_dump(arr, 100, 8, pattern[3]);228229// Not properly aligned. Should automatically down-align by unitsize230do_test_print_hex_dump(arr + 1, 100, 2, pattern[1]);231do_test_print_hex_dump(arr + 1, 100, 4, pattern[2]);232do_test_print_hex_dump(arr + 1, 100, 8, pattern[3]);233234os::free(arr);235}236237//////////////////////////////////////////////////////////////////////////////238// Test os::vsnprintf and friends.239240static void check_snprintf_result(int expected, size_t limit, int actual, bool expect_count) {241if (expect_count || ((size_t)expected < limit)) {242ASSERT_EQ(expected, actual);243} else {244ASSERT_GT(0, actual);245}246}247248// PrintFn is expected to be int (*)(char*, size_t, const char*, ...).249// But jio_snprintf is a C-linkage function with that signature, which250// has a different type on some platforms (like Solaris).251template<typename PrintFn>252static void test_snprintf(PrintFn pf, bool expect_count) {253const char expected[] = "abcdefghijklmnopqrstuvwxyz";254const int expected_len = sizeof(expected) - 1;255const size_t padding_size = 10;256char buffer[2 * (sizeof(expected) + padding_size)];257char check_buffer[sizeof(buffer)];258const char check_char = '1'; // Something not in expected.259memset(check_buffer, check_char, sizeof(check_buffer));260const size_t sizes_to_test[] = {261sizeof(buffer) - padding_size, // Fits, with plenty of space to spare.262sizeof(buffer)/2, // Fits, with space to spare.263sizeof(buffer)/4, // Doesn't fit.264sizeof(expected) + padding_size + 1, // Fits, with a little room to spare265sizeof(expected) + padding_size, // Fits exactly.266sizeof(expected) + padding_size - 1, // Doesn't quite fit.2672, // One char + terminating NUL.2681, // Only space for terminating NUL.2690 }; // No space at all.270for (unsigned i = 0; i < ARRAY_SIZE(sizes_to_test); ++i) {271memset(buffer, check_char, sizeof(buffer)); // To catch stray writes.272size_t test_size = sizes_to_test[i];273ResourceMark rm;274stringStream s;275s.print("test_size: " SIZE_FORMAT, test_size);276SCOPED_TRACE(s.as_string());277size_t prefix_size = padding_size;278guarantee(test_size <= (sizeof(buffer) - prefix_size), "invariant");279size_t write_size = MIN2(sizeof(expected), test_size);280size_t suffix_size = sizeof(buffer) - prefix_size - write_size;281char* write_start = buffer + prefix_size;282char* write_end = write_start + write_size;283284int result = pf(write_start, test_size, "%s", expected);285286check_snprintf_result(expected_len, test_size, result, expect_count);287288// Verify expected output.289if (test_size > 0) {290ASSERT_EQ(0, strncmp(write_start, expected, write_size - 1));291// Verify terminating NUL of output.292ASSERT_EQ('\0', write_start[write_size - 1]);293} else {294guarantee(test_size == 0, "invariant");295guarantee(write_size == 0, "invariant");296guarantee(prefix_size + suffix_size == sizeof(buffer), "invariant");297guarantee(write_start == write_end, "invariant");298}299300// Verify no scribbling on prefix or suffix.301ASSERT_EQ(0, strncmp(buffer, check_buffer, prefix_size));302ASSERT_EQ(0, strncmp(write_end, check_buffer, suffix_size));303}304305// Special case of 0-length buffer with empty (except for terminator) output.306check_snprintf_result(0, 0, pf(NULL, 0, "%s", ""), expect_count);307check_snprintf_result(0, 0, pf(NULL, 0, ""), expect_count);308}309310// This is probably equivalent to os::snprintf, but we're being311// explicit about what we're testing here.312static int vsnprintf_wrapper(char* buf, size_t len, const char* fmt, ...) {313va_list args;314va_start(args, fmt);315int result = os::vsnprintf(buf, len, fmt, args);316va_end(args);317return result;318}319320TEST_VM(os, vsnprintf) {321test_snprintf(vsnprintf_wrapper, true);322}323324TEST_VM(os, snprintf) {325test_snprintf(os::snprintf, true);326}327328// These are declared in jvm.h; test here, with related functions.329extern "C" {330int jio_vsnprintf(char*, size_t, const char*, va_list);331int jio_snprintf(char*, size_t, const char*, ...);332}333334// This is probably equivalent to jio_snprintf, but we're being335// explicit about what we're testing here.336static int jio_vsnprintf_wrapper(char* buf, size_t len, const char* fmt, ...) {337va_list args;338va_start(args, fmt);339int result = jio_vsnprintf(buf, len, fmt, args);340va_end(args);341return result;342}343344TEST_VM(os, jio_vsnprintf) {345test_snprintf(jio_vsnprintf_wrapper, false);346}347348TEST_VM(os, jio_snprintf) {349test_snprintf(jio_snprintf, false);350}351352// Test that os::release_memory() can deal with areas containing multiple mappings.353#define PRINT_MAPPINGS(s) { tty->print_cr("%s", s); os::print_memory_mappings((char*)p, total_range_len, tty); }354//#define PRINT_MAPPINGS355356#ifndef _AIX // JDK-8257041357// Reserve an area consisting of multiple mappings358// (from multiple calls to os::reserve_memory)359static address reserve_multiple(int num_stripes, size_t stripe_len) {360assert(is_aligned(stripe_len, os::vm_allocation_granularity()), "Sanity");361size_t total_range_len = num_stripes * stripe_len;362// Reserve a large contiguous area to get the address space...363address p = (address)os::reserve_memory(total_range_len);364EXPECT_NE(p, (address)NULL);365// .. release it...366EXPECT_TRUE(os::release_memory((char*)p, total_range_len));367// ... re-reserve in the same spot multiple areas...368for (int stripe = 0; stripe < num_stripes; stripe++) {369address q = p + (stripe * stripe_len);370// Commit, alternatingly with or without exec permission,371// to prevent kernel from folding these mappings.372const bool executable = stripe % 2 == 0;373q = (address)os::attempt_reserve_memory_at((char*)q, stripe_len, executable);374EXPECT_NE(q, (address)NULL);375EXPECT_TRUE(os::commit_memory((char*)q, stripe_len, executable));376}377return p;378}379#endif // !AIX380381// Reserve an area with a single call to os::reserve_memory,382// with multiple committed and uncommitted regions383static address reserve_one_commit_multiple(int num_stripes, size_t stripe_len) {384assert(is_aligned(stripe_len, os::vm_allocation_granularity()), "Sanity");385size_t total_range_len = num_stripes * stripe_len;386address p = (address)os::reserve_memory(total_range_len);387EXPECT_NE(p, (address)NULL);388for (int stripe = 0; stripe < num_stripes; stripe++) {389address q = p + (stripe * stripe_len);390if (stripe % 2 == 0) {391EXPECT_TRUE(os::commit_memory((char*)q, stripe_len, false));392}393}394return p;395}396397#ifdef _WIN32398// Release a range allocated with reserve_multiple carefully, to not trip mapping399// asserts on Windows in os::release_memory()400static void carefully_release_multiple(address start, int num_stripes, size_t stripe_len) {401for (int stripe = 0; stripe < num_stripes; stripe++) {402address q = start + (stripe * stripe_len);403EXPECT_TRUE(os::release_memory((char*)q, stripe_len));404}405}406struct NUMASwitcher {407const bool _b;408NUMASwitcher(bool v): _b(UseNUMAInterleaving) { UseNUMAInterleaving = v; }409~NUMASwitcher() { UseNUMAInterleaving = _b; }410};411#endif412413#ifndef _AIX // JDK-8257041414#if defined(__APPLE__) && !defined(AARCH64) // JDK-8267339415TEST_VM(os, DISABLED_release_multi_mappings) {416#else417TEST_VM(os, release_multi_mappings) {418#endif419// Test that we can release an area created with multiple reservation calls420const size_t stripe_len = 4 * M;421const int num_stripes = 4;422const size_t total_range_len = stripe_len * num_stripes;423424// reserve address space...425address p = reserve_multiple(num_stripes, stripe_len);426ASSERT_NE(p, (address)NULL);427PRINT_MAPPINGS("A");428429// .. release it...430{431// On Windows, use UseNUMAInterleaving=1 which makes432// os::release_memory accept multi-map-ranges.433// Otherwise we would assert (see below for death test).434WINDOWS_ONLY(NUMASwitcher b(true);)435ASSERT_TRUE(os::release_memory((char*)p, total_range_len));436}437PRINT_MAPPINGS("B");438439// re-reserve it. This should work unless release failed.440address p2 = (address)os::attempt_reserve_memory_at((char*)p, total_range_len);441ASSERT_EQ(p2, p);442PRINT_MAPPINGS("C");443444ASSERT_TRUE(os::release_memory((char*)p, total_range_len));445}446#endif // !AIX447448#ifdef _WIN32449// On Windows, test that we recognize bad ranges.450// On debug this would assert. Test that too.451// On other platforms, we are unable to recognize bad ranges.452#ifdef ASSERT453TEST_VM_ASSERT_MSG(os, release_bad_ranges, ".*bad release") {454#else455TEST_VM(os, release_bad_ranges) {456#endif457char* p = os::reserve_memory(4 * M);458ASSERT_NE(p, (char*)NULL);459// Release part of range460ASSERT_FALSE(os::release_memory(p, M));461// Release part of range462ASSERT_FALSE(os::release_memory(p + M, M));463// Release more than the range (explicitly switch off NUMA here464// to make os::release_memory() test more strictly and to not465// accidentally release neighbors)466{467NUMASwitcher b(false);468ASSERT_FALSE(os::release_memory(p, M * 5));469ASSERT_FALSE(os::release_memory(p - M, M * 5));470ASSERT_FALSE(os::release_memory(p - M, M * 6));471}472473ASSERT_TRUE(os::release_memory(p, 4 * M)); // Release for real474ASSERT_FALSE(os::release_memory(p, 4 * M)); // Again, should fail475}476#endif // _WIN32477478TEST_VM(os, release_one_mapping_multi_commits) {479// Test that we can release an area consisting of interleaved480// committed and uncommitted regions:481const size_t stripe_len = 4 * M;482const int num_stripes = 4;483const size_t total_range_len = stripe_len * num_stripes;484485// reserve address space...486address p = reserve_one_commit_multiple(num_stripes, stripe_len);487ASSERT_NE(p, (address)NULL);488PRINT_MAPPINGS("A");489490// .. release it...491ASSERT_TRUE(os::release_memory((char*)p, total_range_len));492PRINT_MAPPINGS("B");493494// re-reserve it. This should work unless release failed.495address p2 = (address)os::attempt_reserve_memory_at((char*)p, total_range_len);496ASSERT_EQ(p2, p);497PRINT_MAPPINGS("C");498499ASSERT_TRUE(os::release_memory((char*)p, total_range_len));500PRINT_MAPPINGS("D");501}502503static void test_show_mappings(address start, size_t size) {504// Note: should this overflow, thats okay. stream will silently truncate. Does not matter for the test.505const size_t buflen = 4 * M;506char* buf = NEW_C_HEAP_ARRAY(char, buflen, mtInternal);507buf[0] = '\0';508stringStream ss(buf, buflen);509if (start != nullptr) {510os::print_memory_mappings((char*)start, size, &ss);511} else {512os::print_memory_mappings(&ss); // prints full address space513}514// Still an empty implementation on MacOS and AIX515#if defined(LINUX) || defined(_WIN32)516EXPECT_NE(buf[0], '\0');517#endif518// buf[buflen - 1] = '\0';519// tty->print_raw(buf);520FREE_C_HEAP_ARRAY(char, buf);521}522523TEST_VM(os, show_mappings_small_range) {524test_show_mappings((address)0x100000, 2 * G);525}526527TEST_VM(os, show_mappings_full_range) {528// Reserve a small range and fill it with a marker string, should show up529// on implementations displaying range snippets530char* p = os::reserve_memory(1 * M, false, mtInternal);531if (p != nullptr) {532if (os::commit_memory(p, 1 * M, false)) {533strcpy(p, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");534}535}536test_show_mappings(nullptr, 0);537if (p != nullptr) {538os::release_memory(p, 1 * M);539}540}541542#ifdef _WIN32543// Test os::win32::find_mapping544TEST_VM(os, find_mapping_simple) {545const size_t total_range_len = 4 * M;546os::win32::mapping_info_t mapping_info;547548// Some obvious negatives549ASSERT_FALSE(os::win32::find_mapping((address)NULL, &mapping_info));550ASSERT_FALSE(os::win32::find_mapping((address)4711, &mapping_info));551552// A simple allocation553{554address p = (address)os::reserve_memory(total_range_len);555ASSERT_NE(p, (address)NULL);556PRINT_MAPPINGS("A");557for (size_t offset = 0; offset < total_range_len; offset += 4711) {558ASSERT_TRUE(os::win32::find_mapping(p + offset, &mapping_info));559ASSERT_EQ(mapping_info.base, p);560ASSERT_EQ(mapping_info.regions, 1);561ASSERT_EQ(mapping_info.size, total_range_len);562ASSERT_EQ(mapping_info.committed_size, 0);563}564// Test just outside the allocation565if (os::win32::find_mapping(p - 1, &mapping_info)) {566ASSERT_NE(mapping_info.base, p);567}568if (os::win32::find_mapping(p + total_range_len, &mapping_info)) {569ASSERT_NE(mapping_info.base, p);570}571ASSERT_TRUE(os::release_memory((char*)p, total_range_len));572PRINT_MAPPINGS("B");573ASSERT_FALSE(os::win32::find_mapping(p, &mapping_info));574}575}576577TEST_VM(os, find_mapping_2) {578// A more complex allocation, consisting of multiple regions.579const size_t total_range_len = 4 * M;580os::win32::mapping_info_t mapping_info;581582const size_t stripe_len = total_range_len / 4;583address p = reserve_one_commit_multiple(4, stripe_len);584ASSERT_NE(p, (address)NULL);585PRINT_MAPPINGS("A");586for (size_t offset = 0; offset < total_range_len; offset += 4711) {587ASSERT_TRUE(os::win32::find_mapping(p + offset, &mapping_info));588ASSERT_EQ(mapping_info.base, p);589ASSERT_EQ(mapping_info.regions, 4);590ASSERT_EQ(mapping_info.size, total_range_len);591ASSERT_EQ(mapping_info.committed_size, total_range_len / 2);592}593// Test just outside the allocation594if (os::win32::find_mapping(p - 1, &mapping_info)) {595ASSERT_NE(mapping_info.base, p);596}597if (os::win32::find_mapping(p + total_range_len, &mapping_info)) {598ASSERT_NE(mapping_info.base, p);599}600ASSERT_TRUE(os::release_memory((char*)p, total_range_len));601PRINT_MAPPINGS("B");602ASSERT_FALSE(os::win32::find_mapping(p, &mapping_info));603}604605TEST_VM(os, find_mapping_3) {606const size_t total_range_len = 4 * M;607os::win32::mapping_info_t mapping_info;608609// A more complex case, consisting of multiple allocations.610{611const size_t stripe_len = total_range_len / 4;612address p = reserve_multiple(4, stripe_len);613ASSERT_NE(p, (address)NULL);614PRINT_MAPPINGS("E");615for (int stripe = 0; stripe < 4; stripe++) {616ASSERT_TRUE(os::win32::find_mapping(p + (stripe * stripe_len), &mapping_info));617ASSERT_EQ(mapping_info.base, p + (stripe * stripe_len));618ASSERT_EQ(mapping_info.regions, 1);619ASSERT_EQ(mapping_info.size, stripe_len);620ASSERT_EQ(mapping_info.committed_size, stripe_len);621}622carefully_release_multiple(p, 4, stripe_len);623PRINT_MAPPINGS("F");624ASSERT_FALSE(os::win32::find_mapping(p, &mapping_info));625}626}627#endif // _WIN32628629TEST_VM(os, os_pagesizes) {630ASSERT_EQ(os::min_page_size(), 4 * K);631ASSERT_LE(os::min_page_size(), (size_t)os::vm_page_size());632// The vm_page_size should be the smallest in the set of allowed page sizes633// (contract says "default" page size but a lot of code actually assumes634// this to be the smallest page size; notable, deliberate exception is635// AIX which can have smaller page sizes but those are not part of the636// page_sizes() set).637ASSERT_EQ(os::page_sizes().smallest(), (size_t)os::vm_page_size());638// The large page size, if it exists, shall be part of the set639if (UseLargePages) {640ASSERT_GT(os::large_page_size(), (size_t)os::vm_page_size());641ASSERT_TRUE(os::page_sizes().contains(os::large_page_size()));642}643os::page_sizes().print_on(tty);644tty->cr();645}646647static const int min_page_size_log2 = exact_log2(os::min_page_size());648static const int max_page_size_log2 = (int)BitsPerWord;649650TEST_VM(os, pagesizes_test_range) {651for (int bit = min_page_size_log2; bit < max_page_size_log2; bit++) {652for (int bit2 = min_page_size_log2; bit2 < max_page_size_log2; bit2++) {653const size_t s = (size_t)1 << bit;654const size_t s2 = (size_t)1 << bit2;655os::PageSizes pss;656ASSERT_EQ((size_t)0, pss.smallest());657ASSERT_EQ((size_t)0, pss.largest());658// one size set659pss.add(s);660ASSERT_TRUE(pss.contains(s));661ASSERT_EQ(s, pss.smallest());662ASSERT_EQ(s, pss.largest());663ASSERT_EQ(pss.next_larger(s), (size_t)0);664ASSERT_EQ(pss.next_smaller(s), (size_t)0);665// two set666pss.add(s2);667ASSERT_TRUE(pss.contains(s2));668if (s2 < s) {669ASSERT_EQ(s2, pss.smallest());670ASSERT_EQ(s, pss.largest());671ASSERT_EQ(pss.next_larger(s2), (size_t)s);672ASSERT_EQ(pss.next_smaller(s2), (size_t)0);673ASSERT_EQ(pss.next_larger(s), (size_t)0);674ASSERT_EQ(pss.next_smaller(s), (size_t)s2);675} else if (s2 > s) {676ASSERT_EQ(s, pss.smallest());677ASSERT_EQ(s2, pss.largest());678ASSERT_EQ(pss.next_larger(s), (size_t)s2);679ASSERT_EQ(pss.next_smaller(s), (size_t)0);680ASSERT_EQ(pss.next_larger(s2), (size_t)0);681ASSERT_EQ(pss.next_smaller(s2), (size_t)s);682}683for (int bit3 = min_page_size_log2; bit3 < max_page_size_log2; bit3++) {684const size_t s3 = (size_t)1 << bit3;685ASSERT_EQ(s3 == s || s3 == s2, pss.contains(s3));686}687}688}689}690691TEST_VM(os, pagesizes_test_print) {692os::PageSizes pss;693const size_t sizes[] = { 16 * K, 64 * K, 128 * K, 1 * M, 4 * M, 1 * G, 2 * G, 0 };694static const char* const expected = "16k, 64k, 128k, 1M, 4M, 1G, 2G";695for (int i = 0; sizes[i] != 0; i++) {696pss.add(sizes[i]);697}698char buffer[256];699stringStream ss(buffer, sizeof(buffer));700pss.print_on(&ss);701ASSERT_EQ(strcmp(expected, buffer), 0);702}703704TEST_VM(os, dll_address_to_function_and_library_name) {705char tmp[1024];706char output[1024];707stringStream st(output, sizeof(output));708709#define EXPECT_CONTAINS(haystack, needle) \710EXPECT_NE(::strstr(haystack, needle), (char*)NULL)711#define EXPECT_DOES_NOT_CONTAIN(haystack, needle) \712EXPECT_EQ(::strstr(haystack, needle), (char*)NULL)713// #define LOG(...) tty->print_cr(__VA_ARGS__); // enable if needed714#define LOG(...)715716// Invalid addresses717address addr = (address)(intptr_t)-1;718EXPECT_FALSE(os::print_function_and_library_name(&st, addr));719addr = NULL;720EXPECT_FALSE(os::print_function_and_library_name(&st, addr));721722// Valid addresses723// Test with or without shorten-paths, demangle, and scratch buffer724for (int i = 0; i < 16; i++) {725const bool shorten_paths = (i & 1) != 0;726const bool demangle = (i & 2) != 0;727const bool strip_arguments = (i & 4) != 0;728const bool provide_scratch_buffer = (i & 8) != 0;729LOG("shorten_paths=%d, demangle=%d, strip_arguments=%d, provide_scratch_buffer=%d",730shorten_paths, demangle, strip_arguments, provide_scratch_buffer);731732// Should show os::min_page_size in libjvm733addr = CAST_FROM_FN_PTR(address, Threads::create_vm);734st.reset();735EXPECT_TRUE(os::print_function_and_library_name(&st, addr,736provide_scratch_buffer ? tmp : NULL,737sizeof(tmp),738shorten_paths, demangle,739strip_arguments));740EXPECT_CONTAINS(output, "Threads");741EXPECT_CONTAINS(output, "create_vm");742EXPECT_CONTAINS(output, "jvm"); // "jvm.dll" or "libjvm.so" or similar743LOG("%s", output);744745// Test truncation on scratch buffer746if (provide_scratch_buffer) {747st.reset();748tmp[10] = 'X';749EXPECT_TRUE(os::print_function_and_library_name(&st, addr, tmp, 10,750shorten_paths, demangle));751EXPECT_EQ(tmp[10], 'X');752LOG("%s", output);753}754}755}756757// Not a regex! Very primitive, just match:758// "d" - digit759// "a" - ascii760// "." - everything761// rest must match762static bool very_simple_string_matcher(const char* pattern, const char* s) {763const size_t lp = strlen(pattern);764const size_t ls = strlen(s);765if (ls < lp) {766return false;767}768for (size_t i = 0; i < lp; i ++) {769switch (pattern[i]) {770case '.': continue;771case 'd': if (!isdigit(s[i])) return false; break;772case 'a': if (!isascii(s[i])) return false; break;773default: if (s[i] != pattern[i]) return false; break;774}775}776return true;777}778779TEST_VM(os, iso8601_time) {780char buffer[os::iso8601_timestamp_size + 1]; // + space for canary781buffer[os::iso8601_timestamp_size] = 'X'; // canary782const char* result = NULL;783// YYYY-MM-DDThh:mm:ss.mmm+zzzz784const char* const pattern_utc = "dddd-dd-dd.dd:dd:dd.ddd.0000";785const char* const pattern_local = "dddd-dd-dd.dd:dd:dd.ddd.dddd";786787result = os::iso8601_time(buffer, sizeof(buffer), true);788tty->print_cr("%s", result);789EXPECT_EQ(result, buffer);790EXPECT_TRUE(very_simple_string_matcher(pattern_utc, result));791792result = os::iso8601_time(buffer, sizeof(buffer), false);793tty->print_cr("%s", result);794EXPECT_EQ(result, buffer);795EXPECT_TRUE(very_simple_string_matcher(pattern_local, result));796797// Test with explicit timestamps798result = os::iso8601_time(0, buffer, sizeof(buffer), true);799tty->print_cr("%s", result);800EXPECT_EQ(result, buffer);801EXPECT_TRUE(very_simple_string_matcher("1970-01-01.00:00:00.000+0000", result));802803result = os::iso8601_time(17, buffer, sizeof(buffer), true);804tty->print_cr("%s", result);805EXPECT_EQ(result, buffer);806EXPECT_TRUE(very_simple_string_matcher("1970-01-01.00:00:00.017+0000", result));807808// Canary should still be intact809EXPECT_EQ(buffer[os::iso8601_timestamp_size], 'X');810}811812813