1 /* 2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 #include "precompiled.hpp" 25 #include "runtime/os.hpp" 26 #include "unittest.hpp" 27 #include "utilities/globalDefinitions.hpp" 28 29 static ::testing::AssertionResult testPageAddress( 30 const char* expected_addr_expr, 31 const char* addr_expr, 32 const char* page_addr_expr, 33 const char* page_size_expr, 34 const char* actual_addr_expr, 35 address expected_addr, 36 address addr, 37 address page_addr, 38 intptr_t page_size, 39 address actual_addr) { 40 if (expected_addr == actual_addr) { 41 return ::testing::AssertionSuccess(); 42 } 43 44 return ::testing::AssertionFailure() 45 << actual_addr_expr << " returned unexpected address " << (void*) actual_addr << std::endl 46 << "Expected " << expected_addr_expr << ": " << (void*) expected_addr << std::endl 47 << "where" << std::endl 48 << addr_expr << ": " << (void*) addr << std::endl 49 << page_addr_expr << ": " << (void*) page_addr << std::endl 50 << page_size_expr << ": " << page_size; 51 } 52 53 TEST_VM(globalDefinitions, clamp_address_in_page) { 54 const intptr_t page_sizes[] = {os::vm_page_size(), 4096, 8192, 65536, 2 * 1024 * 1024}; 55 const int num_page_sizes = sizeof(page_sizes) / sizeof(page_sizes[0]); 56 57 for (int i = 0; i < num_page_sizes; i++) { 58 intptr_t page_size = page_sizes[i]; 59 address page_address = (address) (10 * page_size); 60 61 const intptr_t within_page_offsets[] = {0, 128, page_size - 1}; 62 const int num_within_page_offsets = sizeof(within_page_offsets) / sizeof(within_page_offsets[0]); 63 64 for (int k = 0; k < num_within_page_offsets; ++k) { 65 address addr = page_address + within_page_offsets[k]; 66 address expected_address = addr; 67 EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size, 68 clamp_address_in_page(addr, page_address, page_size)) 69 << "Expect that address within page is returned as is"; 70 } 71 72 const intptr_t above_page_offsets[] = {page_size, page_size + 1, 5 * page_size + 1}; 73 const int num_above_page_offsets = sizeof(above_page_offsets) / sizeof(above_page_offsets[0]); 74 75 for (int k = 0; k < num_above_page_offsets; ++k) { 76 address addr = page_address + above_page_offsets[k]; 77 address expected_address = page_address + page_size; 78 EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size, 79 clamp_address_in_page(addr, page_address, page_size)) 80 << "Expect that address above page returns start of next page"; 81 } 82 83 const intptr_t below_page_offsets[] = {1, 2 * page_size + 1, 5 * page_size + 1}; 84 const int num_below_page_offsets = sizeof(below_page_offsets) / sizeof(below_page_offsets[0]); 85 86 for (int k = 0; k < num_below_page_offsets; ++k) { 87 address addr = page_address - below_page_offsets[k]; 88 address expected_address = page_address; 89 EXPECT_PRED_FORMAT5(testPageAddress, expected_address, addr, page_address, page_size, 90 clamp_address_in_page(addr, page_address, page_size)) 91 << "Expect that address below page returns start of page"; 92 } 93 } 94 } 95 96 TEST(globalDefinitions, exact_unit_for_byte_size) { 97 EXPECT_STREQ("B", exact_unit_for_byte_size(0)); 98 EXPECT_STREQ("B", exact_unit_for_byte_size(1)); 99 EXPECT_STREQ("B", exact_unit_for_byte_size(K - 1)); 100 EXPECT_STREQ("K", exact_unit_for_byte_size(K)); 101 EXPECT_STREQ("B", exact_unit_for_byte_size(K + 1)); 102 EXPECT_STREQ("B", exact_unit_for_byte_size(M - 1)); 103 EXPECT_STREQ("M", exact_unit_for_byte_size(M)); 104 EXPECT_STREQ("B", exact_unit_for_byte_size(M + 1)); 105 EXPECT_STREQ("K", exact_unit_for_byte_size(M + K)); 106 #ifdef LP64 107 EXPECT_STREQ("B", exact_unit_for_byte_size(G - 1)); 108 EXPECT_STREQ("G", exact_unit_for_byte_size(G)); 109 EXPECT_STREQ("B", exact_unit_for_byte_size(G + 1)); 110 EXPECT_STREQ("K", exact_unit_for_byte_size(G + K)); 111 EXPECT_STREQ("M", exact_unit_for_byte_size(G + M)); 112 EXPECT_STREQ("K", exact_unit_for_byte_size(G + M + K)); 113 #endif 114 } 115 116 TEST(globalDefinitions, byte_size_in_exact_unit) { 117 EXPECT_EQ(0u, byte_size_in_exact_unit(0)); 118 EXPECT_EQ(1u, byte_size_in_exact_unit(1)); 119 EXPECT_EQ(K - 1, byte_size_in_exact_unit(K - 1)); 120 EXPECT_EQ(1u, byte_size_in_exact_unit(K)); 121 EXPECT_EQ(K + 1, byte_size_in_exact_unit(K + 1)); 122 EXPECT_EQ(M - 1, byte_size_in_exact_unit(M - 1)); 123 EXPECT_EQ(1u, byte_size_in_exact_unit(M)); 124 EXPECT_EQ(M + 1, byte_size_in_exact_unit(M + 1)); 125 EXPECT_EQ(K + 1, byte_size_in_exact_unit(M + K)); 126 #ifdef LP64 127 EXPECT_EQ(G - 1, byte_size_in_exact_unit(G - 1)); 128 EXPECT_EQ(1u, byte_size_in_exact_unit(G)); 129 EXPECT_EQ(G + 1, byte_size_in_exact_unit(G + 1)); 130 EXPECT_EQ(M + 1, byte_size_in_exact_unit(G + K)); 131 EXPECT_EQ(K + 1, byte_size_in_exact_unit(G + M)); 132 EXPECT_EQ(M + K + 1, byte_size_in_exact_unit(G + M + K)); 133 #endif 134 }