1 /* 2 * Copyright (c) 2015, 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 25 #include "precompiled.hpp" 26 27 #include <string.h> 28 #include "gc/shared/memset_with_concurrent_readers.hpp" 29 #include "utilities/debug.hpp" 30 #include "utilities/globalDefinitions.hpp" 31 #include "utilities/macros.hpp" 32 #include "utilities/ostream.hpp" 33 34 #if INCLUDE_ALL_GCS 35 36 // Unit test 37 #ifndef PRODUCT 38 39 static unsigned line_byte(const char* line, size_t i) { 40 return unsigned(line[i]) & 0xFF; 41 } 42 43 // Verify memset_with_concurrent_readers mimics memset. 44 // We don't attempt to verify the concurrent reader case. 45 void test_memset_with_concurrent_readers() { 46 const size_t chunk_size = 8 * BytesPerWord; 47 const unsigned chunk_count = 4; 48 const size_t block_size = (chunk_count + 4) * chunk_size; 49 char block[block_size]; 50 char clear_block[block_size]; 51 char set_block[block_size]; 52 53 // block format: 54 // 0: unused leading chunk 55 // 1: chunk written from start index to end of chunk 56 // ... nchunks fully written chunks 57 // N: chunk written from start of chunk to end index 58 // N+1: unused trailing chunk 59 60 const int clear_value = 0; 61 const int set_value = 0xAC; 62 63 memset(clear_block, clear_value, block_size); 64 memset(set_block, set_value, block_size); 65 66 for (unsigned nchunks = 0; nchunks <= chunk_count; ++nchunks) { 67 for (size_t start = 1; start <= chunk_size; ++start) { 68 for (size_t end = 0; end <= chunk_size; ++end) { 69 size_t set_start = chunk_size + start; 70 size_t set_end = (2 + nchunks) * chunk_size + end; 71 size_t set_size = set_end - set_start; 72 73 memset(block, clear_value, block_size); 74 memset_with_concurrent_readers(&block[set_start], set_value, set_size); 75 bool head_clear = !memcmp(clear_block, block, set_start); 76 bool middle_set = !memcmp(set_block, block + set_start, set_size); 77 bool tail_clear = !memcmp(clear_block, block + set_end, block_size - set_end); 78 if (!(head_clear && middle_set && tail_clear)) { 79 tty->print_cr("*** memset_with_concurrent_readers failed: " 80 "set start " SIZE_FORMAT ", set end " SIZE_FORMAT, 81 set_start, set_end); 82 for (unsigned chunk = 0; chunk < (block_size / chunk_size); ++chunk) { 83 for (unsigned line = 0; line < (chunk_size / BytesPerWord); ++line) { 84 const char* lp = &block[chunk * chunk_size + line * BytesPerWord]; 85 tty->print_cr("%d,%d: %2x %2x %2x %2x %2x %2x %2x %2x", 86 chunk, line, 87 line_byte(lp, 0), line_byte(lp, 1), 88 line_byte(lp, 2), line_byte(lp, 3), 89 line_byte(lp, 4), line_byte(lp, 5), 90 line_byte(lp, 6), line_byte(lp, 7)); 91 } 92 } 93 assert(head_clear, "leading byte not clear"); 94 assert(middle_set, "memset byte not set"); 95 assert(tail_clear, "trailing bye not clear"); 96 } 97 } 98 } 99 } 100 } 101 102 #endif // end unit test 103 104 #endif // INCLUDE_ALL_GCS