1 /* 2 * Copyright (c) 2014, 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 /** 26 * @test 27 * @bug 8035968 28 * @summary C2 support for SHA on SPARC 29 * 30 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-1 TestSHA 31 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-224 TestSHA 32 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-256 TestSHA 33 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-384 TestSHA 34 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-512 TestSHA 35 * 36 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-1 -Doffset=1 TestSHA 37 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-224 -Doffset=1 TestSHA 38 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-256 -Doffset=1 TestSHA 39 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-384 -Doffset=1 TestSHA 40 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-512 -Doffset=1 TestSHA 41 * 42 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-1 -Dalgorithm2=SHA-256 TestSHA 43 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-1 -Dalgorithm2=SHA-512 TestSHA 44 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-256 -Dalgorithm2=SHA-512 TestSHA 45 * 46 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=SHA-1 -Dalgorithm2=MD5 TestSHA 47 * @run main/othervm/timeout=600 -Xbatch -Dalgorithm=MD5 -Dalgorithm2=SHA-1 TestSHA 48 */ 49 50 import java.security.MessageDigest; 51 import java.util.Arrays; 52 53 public class TestSHA { 54 private static final int HASH_LEN = 64; /* up to 512-bit */ 55 private static final int ALIGN = 8; /* for different data alignments */ 56 57 public static void main(String[] args) throws Exception { 58 String provider = System.getProperty("provider", "SUN"); 59 String algorithm = System.getProperty("algorithm", "SHA-1"); 60 String algorithm2 = System.getProperty("algorithm2", ""); 61 int msgSize = Integer.getInteger("msgSize", 1024); 62 int offset = Integer.getInteger("offset", 0) % ALIGN; 63 int iters = (args.length > 0 ? Integer.valueOf(args[0]) : 100000); 64 int warmupIters = (args.length > 1 ? Integer.valueOf(args[1]) : 20000); 65 66 testSHA(provider, algorithm, msgSize, offset, iters, warmupIters); 67 68 if (algorithm2.equals("") == false) { 69 testSHA(provider, algorithm2, msgSize, offset, iters, warmupIters); 70 } 71 } 72 73 static void testSHA(String provider, String algorithm, int msgSize, 74 int offset, int iters, int warmupIters) throws Exception { 75 System.out.println("provider = " + provider); 76 System.out.println("algorithm = " + algorithm); 77 System.out.println("msgSize = " + msgSize + " bytes"); 78 System.out.println("offset = " + offset); 79 System.out.println("iters = " + iters); 80 81 byte[] expectedHash = new byte[HASH_LEN]; 82 byte[] hash = new byte[HASH_LEN]; 83 byte[] data = new byte[msgSize + offset]; 84 for (int i = 0; i < (msgSize + offset); i++) { 85 data[i] = (byte)(i & 0xff); 86 } 87 88 try { 89 MessageDigest sha = MessageDigest.getInstance(algorithm, provider); 90 91 /* do once, which doesn't use intrinsics */ 92 sha.reset(); 93 sha.update(data, offset, msgSize); 94 expectedHash = sha.digest(); 95 96 /* warm up */ 97 for (int i = 0; i < warmupIters; i++) { 98 sha.reset(); 99 sha.update(data, offset, msgSize); 100 hash = sha.digest(); 101 } 102 103 /* check result */ 104 if (Arrays.equals(hash, expectedHash) == false) { 105 System.out.println("TestSHA Error: "); 106 showArray(expectedHash, "expectedHash"); 107 showArray(hash, "computedHash"); 108 //System.exit(1); 109 throw new Exception("TestSHA Error"); 110 } else { 111 showArray(hash, "hash"); 112 } 113 114 /* measure performance */ 115 long start = System.nanoTime(); 116 for (int i = 0; i < iters; i++) { 117 sha.reset(); 118 sha.update(data, offset, msgSize); 119 hash = sha.digest(); 120 } 121 long end = System.nanoTime(); 122 double total = (double)(end - start)/1e9; /* in seconds */ 123 double thruput = (double)msgSize*iters/1e6/total; /* in MB/s */ 124 System.out.println("TestSHA runtime = " + total + " seconds"); 125 System.out.println("TestSHA throughput = " + thruput + " MB/s"); 126 System.out.println(); 127 } catch (Exception e) { 128 System.out.println("Exception: " + e); 129 //System.exit(1); 130 throw new Exception(e); 131 } 132 } 133 134 static void showArray(byte b[], String name) { 135 System.out.format("%s [%d]: ", name, b.length); 136 for (int i = 0; i < Math.min(b.length, HASH_LEN); i++) { 137 System.out.format("%02x ", b[i] & 0xff); 138 } 139 System.out.println(); 140 } 141 }