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 * @test 26 * @bug 8073583 27 * @summary C2 support for CRC32C on SPARC 28 * 29 * @run main/othervm/timeout=600 -Xbatch TestCRC32C -m 30 */ 31 32 import java.nio.ByteBuffer; 33 import java.util.zip.Checksum; 34 import java.util.zip.CRC32C; 35 36 public class TestCRC32C { 37 public static void main(String[] args) { 38 int offset = Integer.getInteger("offset", 0); 39 int msgSize = Integer.getInteger("msgSize", 512); 40 boolean multi = false; 41 int iters = 20000; 42 int warmupIters = 20000; 43 44 if (args.length > 0) { 45 if (args[0].equals("-m")) { 46 multi = true; 47 } else { 48 iters = Integer.valueOf(args[0]); 49 } 50 if (args.length > 1) { 51 warmupIters = Integer.valueOf(args[1]); 52 } 53 } 54 55 if (multi) { 56 test_multi(warmupIters); 57 return; 58 } 59 60 System.out.println(" offset = " + offset); 61 System.out.println("msgSize = " + msgSize + " bytes"); 62 System.out.println(" iters = " + iters); 63 64 byte[] b = initializedBytes(msgSize, offset); 65 66 CRC32C crc0 = new CRC32C(); 67 CRC32C crc1 = new CRC32C(); 68 CRC32C crc2 = new CRC32C(); 69 70 crc0.update(b, offset, msgSize); 71 72 System.out.println("-------------------------------------------------------"); 73 74 /* warm up */ 75 for (int i = 0; i < warmupIters; i++) { 76 crc1.reset(); 77 crc1.update(b, offset, msgSize); 78 } 79 80 /* measure performance */ 81 long start = System.nanoTime(); 82 for (int i = 0; i < iters; i++) { 83 crc1.reset(); 84 crc1.update(b, offset, msgSize); 85 } 86 long end = System.nanoTime(); 87 double total = (double)(end - start)/1e9; // in seconds 88 double thruput = (double)msgSize*iters/1e6/total; // in MB/s 89 System.out.println("CRC32C.update(byte[]) runtime = " + total + " seconds"); 90 System.out.println("CRC32C.update(byte[]) throughput = " + thruput + " MB/s"); 91 92 /* check correctness */ 93 for (int i = 0; i < iters; i++) { 94 crc1.reset(); 95 crc1.update(b, offset, msgSize); 96 if (!check(crc0, crc1)) break; 97 } 98 report("CRCs", crc0, crc1); 99 100 System.out.println("-------------------------------------------------------"); 101 102 ByteBuffer buf = ByteBuffer.allocateDirect(msgSize); 103 buf.put(b, offset, msgSize); 104 buf.flip(); 105 106 /* warm up */ 107 for (int i = 0; i < warmupIters; i++) { 108 crc2.reset(); 109 crc2.update(buf); 110 buf.rewind(); 111 } 112 113 /* measure performance */ 114 start = System.nanoTime(); 115 for (int i = 0; i < iters; i++) { 116 crc2.reset(); 117 crc2.update(buf); 118 buf.rewind(); 119 } 120 end = System.nanoTime(); 121 total = (double)(end - start)/1e9; // in seconds 122 thruput = (double)msgSize*iters/1e6/total; // in MB/s 123 System.out.println("CRC32C.update(ByteBuffer) runtime = " + total + " seconds"); 124 System.out.println("CRC32C.update(ByteBuffer) throughput = " + thruput + " MB/s"); 125 126 /* check correctness */ 127 for (int i = 0; i < iters; i++) { 128 crc2.reset(); 129 crc2.update(buf); 130 buf.rewind(); 131 if (!check(crc0, crc2)) break; 132 } 133 report("CRCs", crc0, crc2); 134 135 System.out.println("-------------------------------------------------------"); 136 } 137 138 private static void report(String s, Checksum crc0, Checksum crc1) { 139 System.out.printf("%s: crc0 = %08x, crc1 = %08x\n", 140 s, crc0.getValue(), crc1.getValue()); 141 } 142 143 private static boolean check(Checksum crc0, Checksum crc1) { 144 if (crc0.getValue() != crc1.getValue()) { 145 System.err.printf("ERROR: crc0 = %08x, crc1 = %08x\n", 146 crc0.getValue(), crc1.getValue()); 147 return false; 148 } 149 return true; 150 } 151 152 private static byte[] initializedBytes(int M, int offset) { 153 byte[] bytes = new byte[M + offset]; 154 for (int i = 0; i < offset; i++) { 155 bytes[i] = (byte) i; 156 } 157 for (int i = offset; i < bytes.length; i++) { 158 bytes[i] = (byte) (i - offset); 159 } 160 return bytes; 161 } 162 163 private static void test_multi(int iters) { 164 int len1 = 8; // the 8B/iteration loop 165 int len2 = 32; // the 32B/iteration loop 166 int len3 = 4096; // the 4KB/iteration loop 167 168 byte[] b = initializedBytes(len3*16, 0); 169 int[] offsets = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 16, 32, 64, 128, 256, 512 }; 170 int[] sizes = { 0, 1, 2, 3, 4, 5, 6, 7, 171 len1, len1+1, len1+2, len1+3, len1+4, len1+5, len1+6, len1+7, 172 len1*2, len1*2+1, len1*2+3, len1*2+5, len1*2+7, 173 len2, len2+1, len2+3, len2+5, len2+7, 174 len2*2, len2*4, len2*8, len2*16, len2*32, len2*64, 175 len3, len3+1, len3+3, len3+5, len3+7, 176 len3*2, len3*4, len3*8, 177 len1+len2, len1+len2+1, len1+len2+3, len1+len2+5, len1+len2+7, 178 len1+len3, len1+len3+1, len1+len3+3, len1+len3+5, len1+len3+7, 179 len2+len3, len2+len3+1, len2+len3+3, len2+len3+5, len2+len3+7, 180 len1+len2+len3, len1+len2+len3+1, len1+len2+len3+3, 181 len1+len2+len3+5, len1+len2+len3+7, 182 (len1+len2+len3)*2, (len1+len2+len3)*2+1, (len1+len2+len3)*2+3, 183 (len1+len2+len3)*2+5, (len1+len2+len3)*2+7, 184 (len1+len2+len3)*3, (len1+len2+len3)*3-1, (len1+len2+len3)*3-3, 185 (len1+len2+len3)*3-5, (len1+len2+len3)*3-7 }; 186 CRC32C[] crc0 = new CRC32C[offsets.length*sizes.length]; 187 CRC32C[] crc1 = new CRC32C[offsets.length*sizes.length]; 188 int i, j, k; 189 190 System.out.printf("testing %d cases ...\n", offsets.length*sizes.length); 191 192 /* set the result from interpreter as reference */ 193 for (i = 0; i < offsets.length; i++) { 194 for (j = 0; j < sizes.length; j++) { 195 crc0[i*sizes.length + j] = new CRC32C(); 196 crc1[i*sizes.length + j] = new CRC32C(); 197 crc0[i*sizes.length + j].update(b, offsets[i], sizes[j]); 198 } 199 } 200 201 /* warm up the JIT compiler and get result */ 202 for (k = 0; k < iters; k++) { 203 for (i = 0; i < offsets.length; i++) { 204 for (j = 0; j < sizes.length; j++) { 205 crc1[i*sizes.length + j].reset(); 206 crc1[i*sizes.length + j].update(b, offsets[i], sizes[j]); 207 } 208 } 209 } 210 211 /* check correctness */ 212 for (i = 0; i < offsets.length; i++) { 213 for (j = 0; j < sizes.length; j++) { 214 if (!check(crc0[i*sizes.length + j], crc1[i*sizes.length + j])) { 215 System.out.printf("offsets[%d] = %d", i, offsets[i]); 216 System.out.printf("\tsizes[%d] = %d\n", j, sizes[j]); 217 } 218 } 219 } 220 } 221 }