Path: blob/master/test/hotspot/gtest/runtime/test_os.cpp
64438 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 "services/memTracker.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#include "runtime/frame.inline.hpp"3536static size_t small_page_size() {37return os::vm_page_size();38}3940static size_t large_page_size() {41const size_t large_page_size_example = 4 * M;42return os::page_size_for_region_aligned(large_page_size_example, 1);43}4445TEST_VM(os, page_size_for_region) {46size_t large_page_example = 4 * M;47size_t large_page = os::page_size_for_region_aligned(large_page_example, 1);4849size_t small_page = os::vm_page_size();50if (large_page > small_page) {51size_t num_small_in_large = large_page / small_page;52size_t page = os::page_size_for_region_aligned(large_page, num_small_in_large);53ASSERT_EQ(page, small_page) << "Did not get a small page";54}55}5657TEST_VM(os, page_size_for_region_aligned) {58if (UseLargePages) {59const size_t small_page = small_page_size();60const size_t large_page = large_page_size();6162if (large_page > small_page) {63size_t num_small_pages_in_large = large_page / small_page;64size_t page = os::page_size_for_region_aligned(large_page, num_small_pages_in_large);6566ASSERT_EQ(page, small_page);67}68}69}7071TEST_VM(os, page_size_for_region_alignment) {72if (UseLargePages) {73const size_t small_page = small_page_size();74const size_t large_page = large_page_size();75if (large_page > small_page) {76const size_t unaligned_region = large_page + 17;77size_t page = os::page_size_for_region_aligned(unaligned_region, 1);78ASSERT_EQ(page, small_page);7980const size_t num_pages = 5;81const size_t aligned_region = large_page * num_pages;82page = os::page_size_for_region_aligned(aligned_region, num_pages);83ASSERT_EQ(page, large_page);84}85}86}8788TEST_VM(os, page_size_for_region_unaligned) {89if (UseLargePages) {90// Given exact page size, should return that page size.91for (size_t s = os::page_sizes().largest(); s != 0; s = os::page_sizes().next_smaller(s)) {92size_t actual = os::page_size_for_region_unaligned(s, 1);93ASSERT_EQ(s, actual);94}9596// Given slightly larger size than a page size, return the page size.97for (size_t s = os::page_sizes().largest(); s != 0; s = os::page_sizes().next_smaller(s)) {98size_t actual = os::page_size_for_region_unaligned(s + 17, 1);99ASSERT_EQ(s, actual);100}101102// Given a slightly smaller size than a page size,103// return the next smaller page size.104for (size_t s = os::page_sizes().largest(); s != 0; s = os::page_sizes().next_smaller(s)) {105const size_t expected = os::page_sizes().next_smaller(s);106if (expected != 0) {107size_t actual = os::page_size_for_region_unaligned(s - 17, 1);108ASSERT_EQ(actual, expected);109}110}111112// Return small page size for values less than a small page.113size_t small_page = os::page_sizes().smallest();114size_t actual = os::page_size_for_region_unaligned(small_page - 17, 1);115ASSERT_EQ(small_page, actual);116}117}118119TEST(os, test_random) {120const double m = 2147483647;121double mean = 0.0, variance = 0.0, t;122const int reps = 10000;123unsigned int seed = 1;124125// tty->print_cr("seed %ld for %ld repeats...", seed, reps);126int num;127for (int k = 0; k < reps; k++) {128// Use next_random so the calculation is stateless.129num = seed = os::next_random(seed);130double u = (double)num / m;131ASSERT_TRUE(u >= 0.0 && u <= 1.0) << "bad random number!";132133// calculate mean and variance of the random sequence134mean += u;135variance += (u*u);136}137mean /= reps;138variance /= (reps - 1);139140ASSERT_EQ(num, 1043618065) << "bad seed";141// tty->print_cr("mean of the 1st 10000 numbers: %f", mean);142int intmean = mean*100;143ASSERT_EQ(intmean, 50);144// tty->print_cr("variance of the 1st 10000 numbers: %f", variance);145int intvariance = variance*100;146ASSERT_EQ(intvariance, 33);147const double eps = 0.0001;148t = fabsd(mean - 0.5018);149ASSERT_LT(t, eps) << "bad mean";150t = (variance - 0.3355) < 0.0 ? -(variance - 0.3355) : variance - 0.3355;151ASSERT_LT(t, eps) << "bad variance";152}153154#ifdef ASSERT155TEST_VM_ASSERT_MSG(os, page_size_for_region_with_zero_min_pages,156"assert.min_pages > 0. failed: sanity") {157size_t region_size = 16 * os::vm_page_size();158os::page_size_for_region_aligned(region_size, 0); // should assert159}160#endif161162static void do_test_print_hex_dump(address addr, size_t len, int unitsize, const char* expected) {163char buf[256];164buf[0] = '\0';165stringStream ss(buf, sizeof(buf));166os::print_hex_dump(&ss, addr, addr + len, unitsize);167// tty->print_cr("expected: %s", expected);168// tty->print_cr("result: %s", buf);169ASSERT_NE(strstr(buf, expected), (char*)NULL);170}171172TEST_VM(os, test_print_hex_dump) {173const char* pattern [4] = {174#ifdef VM_LITTLE_ENDIAN175"00 01 02 03 04 05 06 07",176"0100 0302 0504 0706",177"03020100 07060504",178"0706050403020100"179#else180"00 01 02 03 04 05 06 07",181"0001 0203 0405 0607",182"00010203 04050607",183"0001020304050607"184#endif185};186187const char* pattern_not_readable [4] = {188"?? ?? ?? ?? ?? ?? ?? ??",189"???? ???? ???? ????",190"???????? ????????",191"????????????????"192};193194// On AIX, zero page is readable.195address unreadable =196#ifdef AIX197(address) 0xFFFFFFFFFFFF0000ULL;198#else199(address) 0200#endif201;202203ResourceMark rm;204char buf[64];205stringStream ss(buf, sizeof(buf));206outputStream* out = &ss;207// outputStream* out = tty; // enable for printout208209// Test dumping unreadable memory210// Exclude test for Windows for now, since it needs SEH handling to work which cannot be211// guaranteed when we call directly into VM code. (see JDK-8220220)212#ifndef _WIN32213do_test_print_hex_dump(unreadable, 100, 1, pattern_not_readable[0]);214do_test_print_hex_dump(unreadable, 100, 2, pattern_not_readable[1]);215do_test_print_hex_dump(unreadable, 100, 4, pattern_not_readable[2]);216do_test_print_hex_dump(unreadable, 100, 8, pattern_not_readable[3]);217#endif218219// Test dumping readable memory220address arr = (address)os::malloc(100, mtInternal);221for (int c = 0; c < 100; c++) {222arr[c] = c;223}224225// properly aligned226do_test_print_hex_dump(arr, 100, 1, pattern[0]);227do_test_print_hex_dump(arr, 100, 2, pattern[1]);228do_test_print_hex_dump(arr, 100, 4, pattern[2]);229do_test_print_hex_dump(arr, 100, 8, pattern[3]);230231// Not properly aligned. Should automatically down-align by unitsize232do_test_print_hex_dump(arr + 1, 100, 2, pattern[1]);233do_test_print_hex_dump(arr + 1, 100, 4, pattern[2]);234do_test_print_hex_dump(arr + 1, 100, 8, pattern[3]);235236os::free(arr);237}238239//////////////////////////////////////////////////////////////////////////////240// Test os::vsnprintf and friends.241242static void check_snprintf_result(int expected, size_t limit, int actual, bool expect_count) {243if (expect_count || ((size_t)expected < limit)) {244ASSERT_EQ(expected, actual);245} else {246ASSERT_GT(0, actual);247}248}249250// PrintFn is expected to be int (*)(char*, size_t, const char*, ...).251// But jio_snprintf is a C-linkage function with that signature, which252// has a different type on some platforms (like Solaris).253template<typename PrintFn>254static void test_snprintf(PrintFn pf, bool expect_count) {255const char expected[] = "abcdefghijklmnopqrstuvwxyz";256const int expected_len = sizeof(expected) - 1;257const size_t padding_size = 10;258char buffer[2 * (sizeof(expected) + padding_size)];259char check_buffer[sizeof(buffer)];260const char check_char = '1'; // Something not in expected.261memset(check_buffer, check_char, sizeof(check_buffer));262const size_t sizes_to_test[] = {263sizeof(buffer) - padding_size, // Fits, with plenty of space to spare.264sizeof(buffer)/2, // Fits, with space to spare.265sizeof(buffer)/4, // Doesn't fit.266sizeof(expected) + padding_size + 1, // Fits, with a little room to spare267sizeof(expected) + padding_size, // Fits exactly.268sizeof(expected) + padding_size - 1, // Doesn't quite fit.2692, // One char + terminating NUL.2701, // Only space for terminating NUL.2710 }; // No space at all.272for (unsigned i = 0; i < ARRAY_SIZE(sizes_to_test); ++i) {273memset(buffer, check_char, sizeof(buffer)); // To catch stray writes.274size_t test_size = sizes_to_test[i];275ResourceMark rm;276stringStream s;277s.print("test_size: " SIZE_FORMAT, test_size);278SCOPED_TRACE(s.as_string());279size_t prefix_size = padding_size;280guarantee(test_size <= (sizeof(buffer) - prefix_size), "invariant");281size_t write_size = MIN2(sizeof(expected), test_size);282size_t suffix_size = sizeof(buffer) - prefix_size - write_size;283char* write_start = buffer + prefix_size;284char* write_end = write_start + write_size;285286int result = pf(write_start, test_size, "%s", expected);287288check_snprintf_result(expected_len, test_size, result, expect_count);289290// Verify expected output.291if (test_size > 0) {292ASSERT_EQ(0, strncmp(write_start, expected, write_size - 1));293// Verify terminating NUL of output.294ASSERT_EQ('\0', write_start[write_size - 1]);295} else {296guarantee(test_size == 0, "invariant");297guarantee(write_size == 0, "invariant");298guarantee(prefix_size + suffix_size == sizeof(buffer), "invariant");299guarantee(write_start == write_end, "invariant");300}301302// Verify no scribbling on prefix or suffix.303ASSERT_EQ(0, strncmp(buffer, check_buffer, prefix_size));304ASSERT_EQ(0, strncmp(write_end, check_buffer, suffix_size));305}306307// Special case of 0-length buffer with empty (except for terminator) output.308check_snprintf_result(0, 0, pf(NULL, 0, "%s", ""), expect_count);309check_snprintf_result(0, 0, pf(NULL, 0, ""), expect_count);310}311312// This is probably equivalent to os::snprintf, but we're being313// explicit about what we're testing here.314static int vsnprintf_wrapper(char* buf, size_t len, const char* fmt, ...) {315va_list args;316va_start(args, fmt);317int result = os::vsnprintf(buf, len, fmt, args);318va_end(args);319return result;320}321322TEST_VM(os, vsnprintf) {323test_snprintf(vsnprintf_wrapper, true);324}325326TEST_VM(os, snprintf) {327test_snprintf(os::snprintf, true);328}329330// These are declared in jvm.h; test here, with related functions.331extern "C" {332int jio_vsnprintf(char*, size_t, const char*, va_list);333int jio_snprintf(char*, size_t, const char*, ...);334}335336// This is probably equivalent to jio_snprintf, but we're being337// explicit about what we're testing here.338static int jio_vsnprintf_wrapper(char* buf, size_t len, const char* fmt, ...) {339va_list args;340va_start(args, fmt);341int result = jio_vsnprintf(buf, len, fmt, args);342va_end(args);343return result;344}345346TEST_VM(os, jio_vsnprintf) {347test_snprintf(jio_vsnprintf_wrapper, false);348}349350TEST_VM(os, jio_snprintf) {351test_snprintf(jio_snprintf, false);352}353354// Test that os::release_memory() can deal with areas containing multiple mappings.355#define PRINT_MAPPINGS(s) { tty->print_cr("%s", s); os::print_memory_mappings((char*)p, total_range_len, tty); }356//#define PRINT_MAPPINGS357358#ifndef _AIX // JDK-8257041359// Reserve an area consisting of multiple mappings360// (from multiple calls to os::reserve_memory)361static address reserve_multiple(int num_stripes, size_t stripe_len) {362assert(is_aligned(stripe_len, os::vm_allocation_granularity()), "Sanity");363size_t total_range_len = num_stripes * stripe_len;364// Reserve a large contiguous area to get the address space...365address p = (address)os::reserve_memory(total_range_len);366EXPECT_NE(p, (address)NULL);367// .. release it...368EXPECT_TRUE(os::release_memory((char*)p, total_range_len));369// ... re-reserve in the same spot multiple areas...370for (int stripe = 0; stripe < num_stripes; stripe++) {371address q = p + (stripe * stripe_len);372// Commit, alternatingly with or without exec permission,373// to prevent kernel from folding these mappings.374const bool executable = stripe % 2 == 0;375q = (address)os::attempt_reserve_memory_at((char*)q, stripe_len, executable);376EXPECT_NE(q, (address)NULL);377EXPECT_TRUE(os::commit_memory((char*)q, stripe_len, executable));378}379return p;380}381#endif // !AIX382383// Reserve an area with a single call to os::reserve_memory,384// with multiple committed and uncommitted regions385static address reserve_one_commit_multiple(int num_stripes, size_t stripe_len) {386assert(is_aligned(stripe_len, os::vm_allocation_granularity()), "Sanity");387size_t total_range_len = num_stripes * stripe_len;388address p = (address)os::reserve_memory(total_range_len);389EXPECT_NE(p, (address)NULL);390for (int stripe = 0; stripe < num_stripes; stripe++) {391address q = p + (stripe * stripe_len);392if (stripe % 2 == 0) {393EXPECT_TRUE(os::commit_memory((char*)q, stripe_len, false));394}395}396return p;397}398399#ifdef _WIN32400// Release a range allocated with reserve_multiple carefully, to not trip mapping401// asserts on Windows in os::release_memory()402static void carefully_release_multiple(address start, int num_stripes, size_t stripe_len) {403for (int stripe = 0; stripe < num_stripes; stripe++) {404address q = start + (stripe * stripe_len);405EXPECT_TRUE(os::release_memory((char*)q, stripe_len));406}407}408struct NUMASwitcher {409const bool _b;410NUMASwitcher(bool v): _b(UseNUMAInterleaving) { UseNUMAInterleaving = v; }411~NUMASwitcher() { UseNUMAInterleaving = _b; }412};413#endif414415#ifndef _AIX // JDK-8257041416#if defined(__APPLE__) && !defined(AARCH64) // JDK-8267339417TEST_VM(os, DISABLED_release_multi_mappings) {418#else419TEST_VM(os, release_multi_mappings) {420#endif421422// With NMT enabled, this will trigger JDK-8263464. For now disable the test if NMT=on.423if (MemTracker::tracking_level() > NMT_off) {424return;425}426427// Test that we can release an area created with multiple reservation calls428// What we do:429// A) we reserve 6 small segments (stripes) adjacent to each other. We commit430// them with alternating permissions to prevent the kernel from folding them into431// a single segment.432// -stripe-stripe-stripe-stripe-stripe-stripe-433// B) we release the middle four stripes with a single os::release_memory call. This434// tests that os::release_memory indeed works across multiple segments created with435// multiple os::reserve calls.436// -stripe-___________________________-stripe-437// C) Into the now vacated address range between the first and the last stripe, we438// re-reserve a new memory range. We expect this to work as a proof that the address439// range was really released by the single release call (B).440//441// Note that this is inherently racy. Between (B) and (C), some other thread may have442// reserved something into the hole in the meantime. Therefore we keep that range small and443// entrenched between the first and last stripe, which reduces the chance of some concurrent444// thread grabbing that memory.445446const size_t stripe_len = os::vm_allocation_granularity();447const int num_stripes = 6;448const size_t total_range_len = stripe_len * num_stripes;449450// reserve address space...451address p = reserve_multiple(num_stripes, stripe_len);452ASSERT_NE(p, (address)NULL);453PRINT_MAPPINGS("A");454455// .. release the middle stripes...456address p_middle_stripes = p + stripe_len;457const size_t middle_stripe_len = (num_stripes - 2) * stripe_len;458{459// On Windows, temporarily switch on UseNUMAInterleaving to allow release_memory to release460// multiple mappings in one go (otherwise we assert, which we test too, see death test below).461WINDOWS_ONLY(NUMASwitcher b(true);)462ASSERT_TRUE(os::release_memory((char*)p_middle_stripes, middle_stripe_len));463}464PRINT_MAPPINGS("B");465466// ...re-reserve the middle stripes. This should work unless release silently failed.467address p2 = (address)os::attempt_reserve_memory_at((char*)p_middle_stripes, middle_stripe_len);468ASSERT_EQ(p2, p_middle_stripes);469PRINT_MAPPINGS("C");470471// Clean up. Release all mappings.472{473WINDOWS_ONLY(NUMASwitcher b(true);) // allow release_memory to release multiple regions474ASSERT_TRUE(os::release_memory((char*)p, total_range_len));475}476}477#endif // !AIX478479#ifdef _WIN32480// On Windows, test that we recognize bad ranges.481// On debug this would assert. Test that too.482// On other platforms, we are unable to recognize bad ranges.483#ifdef ASSERT484TEST_VM_ASSERT_MSG(os, release_bad_ranges, ".*bad release") {485#else486TEST_VM(os, release_bad_ranges) {487#endif488char* p = os::reserve_memory(4 * M);489ASSERT_NE(p, (char*)NULL);490// Release part of range491ASSERT_FALSE(os::release_memory(p, M));492// Release part of range493ASSERT_FALSE(os::release_memory(p + M, M));494// Release more than the range (explicitly switch off NUMA here495// to make os::release_memory() test more strictly and to not496// accidentally release neighbors)497{498NUMASwitcher b(false);499ASSERT_FALSE(os::release_memory(p, M * 5));500ASSERT_FALSE(os::release_memory(p - M, M * 5));501ASSERT_FALSE(os::release_memory(p - M, M * 6));502}503504ASSERT_TRUE(os::release_memory(p, 4 * M)); // Release for real505ASSERT_FALSE(os::release_memory(p, 4 * M)); // Again, should fail506}507#endif // _WIN32508509TEST_VM(os, release_one_mapping_multi_commits) {510// Test that we can release an area consisting of interleaved511// committed and uncommitted regions:512const size_t stripe_len = 4 * M;513const int num_stripes = 4;514const size_t total_range_len = stripe_len * num_stripes;515516// reserve address space...517address p = reserve_one_commit_multiple(num_stripes, stripe_len);518ASSERT_NE(p, (address)NULL);519PRINT_MAPPINGS("A");520521// .. release it...522ASSERT_TRUE(os::release_memory((char*)p, total_range_len));523PRINT_MAPPINGS("B");524525// re-reserve it. This should work unless release failed.526address p2 = (address)os::attempt_reserve_memory_at((char*)p, total_range_len);527ASSERT_EQ(p2, p);528PRINT_MAPPINGS("C");529530ASSERT_TRUE(os::release_memory((char*)p, total_range_len));531PRINT_MAPPINGS("D");532}533534static void test_show_mappings(address start, size_t size) {535// Note: should this overflow, thats okay. stream will silently truncate. Does not matter for the test.536const size_t buflen = 4 * M;537char* buf = NEW_C_HEAP_ARRAY(char, buflen, mtInternal);538buf[0] = '\0';539stringStream ss(buf, buflen);540if (start != nullptr) {541os::print_memory_mappings((char*)start, size, &ss);542} else {543os::print_memory_mappings(&ss); // prints full address space544}545// Still an empty implementation on MacOS and AIX546#if defined(LINUX) || defined(_WIN32)547EXPECT_NE(buf[0], '\0');548#endif549// buf[buflen - 1] = '\0';550// tty->print_raw(buf);551FREE_C_HEAP_ARRAY(char, buf);552}553554TEST_VM(os, show_mappings_small_range) {555test_show_mappings((address)0x100000, 2 * G);556}557558TEST_VM(os, show_mappings_full_range) {559// Reserve a small range and fill it with a marker string, should show up560// on implementations displaying range snippets561char* p = os::reserve_memory(1 * M, false, mtInternal);562if (p != nullptr) {563if (os::commit_memory(p, 1 * M, false)) {564strcpy(p, "ABCDEFGHIJKLMNOPQRSTUVWXYZ");565}566}567test_show_mappings(nullptr, 0);568if (p != nullptr) {569os::release_memory(p, 1 * M);570}571}572573#ifdef _WIN32574// Test os::win32::find_mapping575TEST_VM(os, find_mapping_simple) {576const size_t total_range_len = 4 * M;577os::win32::mapping_info_t mapping_info;578579// Some obvious negatives580ASSERT_FALSE(os::win32::find_mapping((address)NULL, &mapping_info));581ASSERT_FALSE(os::win32::find_mapping((address)4711, &mapping_info));582583// A simple allocation584{585address p = (address)os::reserve_memory(total_range_len);586ASSERT_NE(p, (address)NULL);587PRINT_MAPPINGS("A");588for (size_t offset = 0; offset < total_range_len; offset += 4711) {589ASSERT_TRUE(os::win32::find_mapping(p + offset, &mapping_info));590ASSERT_EQ(mapping_info.base, p);591ASSERT_EQ(mapping_info.regions, 1);592ASSERT_EQ(mapping_info.size, total_range_len);593ASSERT_EQ(mapping_info.committed_size, 0);594}595// Test just outside the allocation596if (os::win32::find_mapping(p - 1, &mapping_info)) {597ASSERT_NE(mapping_info.base, p);598}599if (os::win32::find_mapping(p + total_range_len, &mapping_info)) {600ASSERT_NE(mapping_info.base, p);601}602ASSERT_TRUE(os::release_memory((char*)p, total_range_len));603PRINT_MAPPINGS("B");604ASSERT_FALSE(os::win32::find_mapping(p, &mapping_info));605}606}607608TEST_VM(os, find_mapping_2) {609// A more complex allocation, consisting of multiple regions.610const size_t total_range_len = 4 * M;611os::win32::mapping_info_t mapping_info;612613const size_t stripe_len = total_range_len / 4;614address p = reserve_one_commit_multiple(4, stripe_len);615ASSERT_NE(p, (address)NULL);616PRINT_MAPPINGS("A");617for (size_t offset = 0; offset < total_range_len; offset += 4711) {618ASSERT_TRUE(os::win32::find_mapping(p + offset, &mapping_info));619ASSERT_EQ(mapping_info.base, p);620ASSERT_EQ(mapping_info.regions, 4);621ASSERT_EQ(mapping_info.size, total_range_len);622ASSERT_EQ(mapping_info.committed_size, total_range_len / 2);623}624// Test just outside the allocation625if (os::win32::find_mapping(p - 1, &mapping_info)) {626ASSERT_NE(mapping_info.base, p);627}628if (os::win32::find_mapping(p + total_range_len, &mapping_info)) {629ASSERT_NE(mapping_info.base, p);630}631ASSERT_TRUE(os::release_memory((char*)p, total_range_len));632PRINT_MAPPINGS("B");633ASSERT_FALSE(os::win32::find_mapping(p, &mapping_info));634}635636TEST_VM(os, find_mapping_3) {637const size_t total_range_len = 4 * M;638os::win32::mapping_info_t mapping_info;639640// A more complex case, consisting of multiple allocations.641{642const size_t stripe_len = total_range_len / 4;643address p = reserve_multiple(4, stripe_len);644ASSERT_NE(p, (address)NULL);645PRINT_MAPPINGS("E");646for (int stripe = 0; stripe < 4; stripe++) {647ASSERT_TRUE(os::win32::find_mapping(p + (stripe * stripe_len), &mapping_info));648ASSERT_EQ(mapping_info.base, p + (stripe * stripe_len));649ASSERT_EQ(mapping_info.regions, 1);650ASSERT_EQ(mapping_info.size, stripe_len);651ASSERT_EQ(mapping_info.committed_size, stripe_len);652}653carefully_release_multiple(p, 4, stripe_len);654PRINT_MAPPINGS("F");655ASSERT_FALSE(os::win32::find_mapping(p, &mapping_info));656}657}658#endif // _WIN32659660TEST_VM(os, os_pagesizes) {661ASSERT_EQ(os::min_page_size(), 4 * K);662ASSERT_LE(os::min_page_size(), (size_t)os::vm_page_size());663// The vm_page_size should be the smallest in the set of allowed page sizes664// (contract says "default" page size but a lot of code actually assumes665// this to be the smallest page size; notable, deliberate exception is666// AIX which can have smaller page sizes but those are not part of the667// page_sizes() set).668ASSERT_EQ(os::page_sizes().smallest(), (size_t)os::vm_page_size());669// The large page size, if it exists, shall be part of the set670if (UseLargePages) {671ASSERT_GT(os::large_page_size(), (size_t)os::vm_page_size());672ASSERT_TRUE(os::page_sizes().contains(os::large_page_size()));673}674os::page_sizes().print_on(tty);675tty->cr();676}677678static const int min_page_size_log2 = exact_log2(os::min_page_size());679static const int max_page_size_log2 = (int)BitsPerWord;680681TEST_VM(os, pagesizes_test_range) {682for (int bit = min_page_size_log2; bit < max_page_size_log2; bit++) {683for (int bit2 = min_page_size_log2; bit2 < max_page_size_log2; bit2++) {684const size_t s = (size_t)1 << bit;685const size_t s2 = (size_t)1 << bit2;686os::PageSizes pss;687ASSERT_EQ((size_t)0, pss.smallest());688ASSERT_EQ((size_t)0, pss.largest());689// one size set690pss.add(s);691ASSERT_TRUE(pss.contains(s));692ASSERT_EQ(s, pss.smallest());693ASSERT_EQ(s, pss.largest());694ASSERT_EQ(pss.next_larger(s), (size_t)0);695ASSERT_EQ(pss.next_smaller(s), (size_t)0);696// two set697pss.add(s2);698ASSERT_TRUE(pss.contains(s2));699if (s2 < s) {700ASSERT_EQ(s2, pss.smallest());701ASSERT_EQ(s, pss.largest());702ASSERT_EQ(pss.next_larger(s2), (size_t)s);703ASSERT_EQ(pss.next_smaller(s2), (size_t)0);704ASSERT_EQ(pss.next_larger(s), (size_t)0);705ASSERT_EQ(pss.next_smaller(s), (size_t)s2);706} else if (s2 > s) {707ASSERT_EQ(s, pss.smallest());708ASSERT_EQ(s2, pss.largest());709ASSERT_EQ(pss.next_larger(s), (size_t)s2);710ASSERT_EQ(pss.next_smaller(s), (size_t)0);711ASSERT_EQ(pss.next_larger(s2), (size_t)0);712ASSERT_EQ(pss.next_smaller(s2), (size_t)s);713}714for (int bit3 = min_page_size_log2; bit3 < max_page_size_log2; bit3++) {715const size_t s3 = (size_t)1 << bit3;716ASSERT_EQ(s3 == s || s3 == s2, pss.contains(s3));717}718}719}720}721722TEST_VM(os, pagesizes_test_print) {723os::PageSizes pss;724const size_t sizes[] = { 16 * K, 64 * K, 128 * K, 1 * M, 4 * M, 1 * G, 2 * G, 0 };725static const char* const expected = "16k, 64k, 128k, 1M, 4M, 1G, 2G";726for (int i = 0; sizes[i] != 0; i++) {727pss.add(sizes[i]);728}729char buffer[256];730stringStream ss(buffer, sizeof(buffer));731pss.print_on(&ss);732ASSERT_EQ(strcmp(expected, buffer), 0);733}734735TEST_VM(os, dll_address_to_function_and_library_name) {736char tmp[1024];737char output[1024];738stringStream st(output, sizeof(output));739740#define EXPECT_CONTAINS(haystack, needle) \741EXPECT_NE(::strstr(haystack, needle), (char*)NULL)742#define EXPECT_DOES_NOT_CONTAIN(haystack, needle) \743EXPECT_EQ(::strstr(haystack, needle), (char*)NULL)744// #define LOG(...) tty->print_cr(__VA_ARGS__); // enable if needed745#define LOG(...)746747// Invalid addresses748LOG("os::print_function_and_library_name(st, -1) expects FALSE.");749address addr = (address)(intptr_t)-1;750EXPECT_FALSE(os::print_function_and_library_name(&st, addr));751LOG("os::print_function_and_library_name(st, NULL) expects FALSE.");752addr = NULL;753EXPECT_FALSE(os::print_function_and_library_name(&st, addr));754755// Valid addresses756// Test with or without shorten-paths, demangle, and scratch buffer757for (int i = 0; i < 16; i++) {758const bool shorten_paths = (i & 1) != 0;759const bool demangle = (i & 2) != 0;760const bool strip_arguments = (i & 4) != 0;761const bool provide_scratch_buffer = (i & 8) != 0;762LOG("shorten_paths=%d, demangle=%d, strip_arguments=%d, provide_scratch_buffer=%d",763shorten_paths, demangle, strip_arguments, provide_scratch_buffer);764765// Should show os::min_page_size in libjvm766addr = CAST_FROM_FN_PTR(address, Threads::create_vm);767st.reset();768EXPECT_TRUE(os::print_function_and_library_name(&st, addr,769provide_scratch_buffer ? tmp : NULL,770sizeof(tmp),771shorten_paths, demangle,772strip_arguments));773EXPECT_CONTAINS(output, "Threads");774EXPECT_CONTAINS(output, "create_vm");775EXPECT_CONTAINS(output, "jvm"); // "jvm.dll" or "libjvm.so" or similar776LOG("%s", output);777778// Test truncation on scratch buffer779if (provide_scratch_buffer) {780st.reset();781tmp[10] = 'X';782EXPECT_TRUE(os::print_function_and_library_name(&st, addr, tmp, 10,783shorten_paths, demangle));784EXPECT_EQ(tmp[10], 'X');785LOG("%s", output);786}787}788}789790// Not a regex! Very primitive, just match:791// "d" - digit792// "a" - ascii793// "." - everything794// rest must match795static bool very_simple_string_matcher(const char* pattern, const char* s) {796const size_t lp = strlen(pattern);797const size_t ls = strlen(s);798if (ls < lp) {799return false;800}801for (size_t i = 0; i < lp; i ++) {802switch (pattern[i]) {803case '.': continue;804case 'd': if (!isdigit(s[i])) return false; break;805case 'a': if (!isascii(s[i])) return false; break;806default: if (s[i] != pattern[i]) return false; break;807}808}809return true;810}811812TEST_VM(os, iso8601_time) {813char buffer[os::iso8601_timestamp_size + 1]; // + space for canary814buffer[os::iso8601_timestamp_size] = 'X'; // canary815const char* result = NULL;816// YYYY-MM-DDThh:mm:ss.mmm+zzzz817const char* const pattern_utc = "dddd-dd-dd.dd:dd:dd.ddd.0000";818const char* const pattern_local = "dddd-dd-dd.dd:dd:dd.ddd.dddd";819820result = os::iso8601_time(buffer, sizeof(buffer), true);821tty->print_cr("%s", result);822EXPECT_EQ(result, buffer);823EXPECT_TRUE(very_simple_string_matcher(pattern_utc, result));824825result = os::iso8601_time(buffer, sizeof(buffer), false);826tty->print_cr("%s", result);827EXPECT_EQ(result, buffer);828EXPECT_TRUE(very_simple_string_matcher(pattern_local, result));829830// Test with explicit timestamps831result = os::iso8601_time(0, buffer, sizeof(buffer), true);832tty->print_cr("%s", result);833EXPECT_EQ(result, buffer);834EXPECT_TRUE(very_simple_string_matcher("1970-01-01.00:00:00.000+0000", result));835836result = os::iso8601_time(17, buffer, sizeof(buffer), true);837tty->print_cr("%s", result);838EXPECT_EQ(result, buffer);839EXPECT_TRUE(very_simple_string_matcher("1970-01-01.00:00:00.017+0000", result));840841// Canary should still be intact842EXPECT_EQ(buffer[os::iso8601_timestamp_size], 'X');843}844845TEST_VM(os, is_first_C_frame) {846#if !defined(_WIN32) && !defined(ZERO)847frame invalid_frame;848EXPECT_TRUE(os::is_first_C_frame(&invalid_frame)); // the frame has zeroes for all values849850frame cur_frame = os::current_frame(); // this frame has to have a sender851EXPECT_FALSE(os::is_first_C_frame(&cur_frame));852#endif // _WIN32853}854855856