Wed, 27 Aug 2014 08:19:12 -0400
8046598: Scalable Native memory tracking development
Summary: Enhance scalability of native memory tracking
Reviewed-by: coleenp, ctornqvi, gtriantafill
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | @test |
aoqi@0 | 26 | @bug 7088419 |
aoqi@0 | 27 | @run main CRCTest |
aoqi@0 | 28 | @summary Use x86 Hardware CRC32 Instruction with java.util.zip.CRC32 and java.util.zip.Adler32 |
aoqi@0 | 29 | */ |
aoqi@0 | 30 | |
aoqi@0 | 31 | import java.nio.ByteBuffer; |
aoqi@0 | 32 | import java.util.zip.CRC32; |
aoqi@0 | 33 | import java.util.zip.Checksum; |
aoqi@0 | 34 | |
aoqi@0 | 35 | public class CRCTest { |
aoqi@0 | 36 | |
aoqi@0 | 37 | public static void main(String[] args) throws Exception { |
aoqi@0 | 38 | |
aoqi@0 | 39 | byte[] b = initializedBytes(4096 * 4096); |
aoqi@0 | 40 | |
aoqi@0 | 41 | { |
aoqi@0 | 42 | CRC32 crc1 = new CRC32(); |
aoqi@0 | 43 | CRC32 crc2 = new CRC32(); |
aoqi@0 | 44 | CRC32 crc3 = new CRC32(); |
aoqi@0 | 45 | CRC32 crc4 = new CRC32(); |
aoqi@0 | 46 | |
aoqi@0 | 47 | crc1.update(b, 0, b.length); |
aoqi@0 | 48 | updateSerial(crc2, b, 0, b.length); |
aoqi@0 | 49 | updateDirect(crc3, b, 0, b.length); |
aoqi@0 | 50 | updateSerialSlow(crc4, b, 0, b.length); |
aoqi@0 | 51 | |
aoqi@0 | 52 | check(crc1, crc2); |
aoqi@0 | 53 | check(crc3, crc4); |
aoqi@0 | 54 | check(crc1, crc3); |
aoqi@0 | 55 | |
aoqi@0 | 56 | crc1.update(17); |
aoqi@0 | 57 | crc2.update(17); |
aoqi@0 | 58 | crc3.update(17); |
aoqi@0 | 59 | crc4.update(17); |
aoqi@0 | 60 | |
aoqi@0 | 61 | crc1.update(b, 1, b.length-2); |
aoqi@0 | 62 | updateSerial(crc2, b, 1, b.length-2); |
aoqi@0 | 63 | updateDirect(crc3, b, 1, b.length-2); |
aoqi@0 | 64 | updateSerialSlow(crc4, b, 1, b.length-2); |
aoqi@0 | 65 | |
aoqi@0 | 66 | check(crc1, crc2); |
aoqi@0 | 67 | check(crc3, crc4); |
aoqi@0 | 68 | check(crc1, crc3); |
aoqi@0 | 69 | |
aoqi@0 | 70 | report("finished huge crc", crc1, crc2, crc3, crc4); |
aoqi@0 | 71 | |
aoqi@0 | 72 | for (int i = 0; i < 256; i++) { |
aoqi@0 | 73 | for (int j = 0; j < 256; j += 1) { |
aoqi@0 | 74 | crc1.update(b, i, j); |
aoqi@0 | 75 | updateSerial(crc2, b, i, j); |
aoqi@0 | 76 | updateDirect(crc3, b, i, j); |
aoqi@0 | 77 | updateSerialSlow(crc4, b, i, j); |
aoqi@0 | 78 | |
aoqi@0 | 79 | check(crc1, crc2); |
aoqi@0 | 80 | check(crc3, crc4); |
aoqi@0 | 81 | check(crc1, crc3); |
aoqi@0 | 82 | |
aoqi@0 | 83 | } |
aoqi@0 | 84 | } |
aoqi@0 | 85 | |
aoqi@0 | 86 | report("finished small survey crc", crc1, crc2, crc3, crc4); |
aoqi@0 | 87 | } |
aoqi@0 | 88 | |
aoqi@0 | 89 | } |
aoqi@0 | 90 | |
aoqi@0 | 91 | private static void report(String s, Checksum crc1, Checksum crc2, |
aoqi@0 | 92 | Checksum crc3, Checksum crc4) { |
aoqi@0 | 93 | System.out.println(s + ", crc1 = " + crc1.getValue() + |
aoqi@0 | 94 | ", crc2 = " + crc2.getValue()+ |
aoqi@0 | 95 | ", crc3 = " + crc3.getValue()+ |
aoqi@0 | 96 | ", crc4 = " + crc4.getValue()); |
aoqi@0 | 97 | } |
aoqi@0 | 98 | |
aoqi@0 | 99 | private static void check(Checksum crc1, Checksum crc2) throws Exception { |
aoqi@0 | 100 | if (crc1.getValue() != crc2.getValue()) { |
aoqi@0 | 101 | String s = "value 1 = " + crc1.getValue() + ", value 2 = " + crc2.getValue(); |
aoqi@0 | 102 | System.err.println(s); |
aoqi@0 | 103 | throw new Exception(s); |
aoqi@0 | 104 | } |
aoqi@0 | 105 | } |
aoqi@0 | 106 | |
aoqi@0 | 107 | private static byte[] initializedBytes(int M) { |
aoqi@0 | 108 | byte[] bytes = new byte[M]; |
aoqi@0 | 109 | for (int i = 0; i < bytes.length; i++) { |
aoqi@0 | 110 | bytes[i] = (byte) i; |
aoqi@0 | 111 | } |
aoqi@0 | 112 | return bytes; |
aoqi@0 | 113 | } |
aoqi@0 | 114 | |
aoqi@0 | 115 | private static void updateSerial(Checksum crc, byte[] b, int start, int length) { |
aoqi@0 | 116 | for (int i = 0; i < length; i++) |
aoqi@0 | 117 | crc.update(b[i+start]); |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | private static void updateSerialSlow(Checksum crc, byte[] b, int start, int length) { |
aoqi@0 | 121 | for (int i = 0; i < length; i++) |
aoqi@0 | 122 | crc.update(b[i+start]); |
aoqi@0 | 123 | crc.getValue(); |
aoqi@0 | 124 | } |
aoqi@0 | 125 | |
aoqi@0 | 126 | private static void updateDirect(CRC32 crc3, byte[] b, int start, int length) { |
aoqi@0 | 127 | ByteBuffer buf = ByteBuffer.allocateDirect(length); |
aoqi@0 | 128 | buf.put(b, start, length); |
aoqi@0 | 129 | buf.flip(); |
aoqi@0 | 130 | crc3.update(buf); |
aoqi@0 | 131 | } |
aoqi@0 | 132 | } |