src/share/vm/classfile/altHashing.cpp

Tue, 08 Aug 2017 15:57:29 +0800

author
aoqi
date
Tue, 08 Aug 2017 15:57:29 +0800
changeset 6876
710a3c8b516e
parent 6351
f9e35a9dc8c7
parent 0
f90c822e73f8
child 9448
73d689add964
permissions
-rw-r--r--

merge

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2012, 2014, 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 #include "precompiled.hpp"
aoqi@0 26 #include "classfile/altHashing.hpp"
aoqi@0 27 #include "classfile/symbolTable.hpp"
aoqi@0 28 #include "classfile/systemDictionary.hpp"
aoqi@0 29 #include "oops/markOop.hpp"
aoqi@0 30 #include "runtime/thread.hpp"
aoqi@0 31
aoqi@0 32 // Get the hash code of the classes mirror if it exists, otherwise just
aoqi@0 33 // return a random number, which is one of the possible hash code used for
aoqi@0 34 // objects. We don't want to call the synchronizer hash code to install
aoqi@0 35 // this value because it may safepoint.
aoqi@0 36 intptr_t object_hash(Klass* k) {
aoqi@0 37 intptr_t hc = k->java_mirror()->mark()->hash();
aoqi@0 38 return hc != markOopDesc::no_hash ? hc : os::random();
aoqi@0 39 }
aoqi@0 40
aoqi@0 41 // Seed value used for each alternative hash calculated.
aoqi@0 42 juint AltHashing::compute_seed() {
aoqi@0 43 jlong nanos = os::javaTimeNanos();
aoqi@0 44 jlong now = os::javaTimeMillis();
aoqi@0 45 int SEED_MATERIAL[8] = {
aoqi@0 46 (int) object_hash(SystemDictionary::String_klass()),
aoqi@0 47 (int) object_hash(SystemDictionary::System_klass()),
aoqi@0 48 (int) os::random(), // current thread isn't a java thread
aoqi@0 49 (int) (((julong)nanos) >> 32),
aoqi@0 50 (int) nanos,
aoqi@0 51 (int) (((julong)now) >> 32),
aoqi@0 52 (int) now,
aoqi@0 53 (int) (os::javaTimeNanos() >> 2)
aoqi@0 54 };
aoqi@0 55
aoqi@0 56 return murmur3_32(SEED_MATERIAL, 8);
aoqi@0 57 }
aoqi@0 58
aoqi@0 59
aoqi@0 60 // Murmur3 hashing for Symbol
aoqi@0 61 juint AltHashing::murmur3_32(juint seed, const jbyte* data, int len) {
aoqi@0 62 juint h1 = seed;
aoqi@0 63 int count = len;
aoqi@0 64 int offset = 0;
aoqi@0 65
aoqi@0 66 // body
aoqi@0 67 while (count >= 4) {
aoqi@0 68 juint k1 = (data[offset] & 0x0FF)
aoqi@0 69 | (data[offset + 1] & 0x0FF) << 8
aoqi@0 70 | (data[offset + 2] & 0x0FF) << 16
aoqi@0 71 | data[offset + 3] << 24;
aoqi@0 72
aoqi@0 73 count -= 4;
aoqi@0 74 offset += 4;
aoqi@0 75
aoqi@0 76 k1 *= 0xcc9e2d51;
aoqi@0 77 k1 = Integer_rotateLeft(k1, 15);
aoqi@0 78 k1 *= 0x1b873593;
aoqi@0 79
aoqi@0 80 h1 ^= k1;
aoqi@0 81 h1 = Integer_rotateLeft(h1, 13);
aoqi@0 82 h1 = h1 * 5 + 0xe6546b64;
aoqi@0 83 }
aoqi@0 84
aoqi@0 85 // tail
aoqi@0 86
aoqi@0 87 if (count > 0) {
aoqi@0 88 juint k1 = 0;
aoqi@0 89
aoqi@0 90 switch (count) {
aoqi@0 91 case 3:
aoqi@0 92 k1 ^= (data[offset + 2] & 0xff) << 16;
aoqi@0 93 // fall through
aoqi@0 94 case 2:
aoqi@0 95 k1 ^= (data[offset + 1] & 0xff) << 8;
aoqi@0 96 // fall through
aoqi@0 97 case 1:
aoqi@0 98 k1 ^= (data[offset] & 0xff);
aoqi@0 99 // fall through
aoqi@0 100 default:
aoqi@0 101 k1 *= 0xcc9e2d51;
aoqi@0 102 k1 = Integer_rotateLeft(k1, 15);
aoqi@0 103 k1 *= 0x1b873593;
aoqi@0 104 h1 ^= k1;
aoqi@0 105 }
aoqi@0 106 }
aoqi@0 107
aoqi@0 108 // finalization
aoqi@0 109 h1 ^= len;
aoqi@0 110
aoqi@0 111 // finalization mix force all bits of a hash block to avalanche
aoqi@0 112 h1 ^= h1 >> 16;
aoqi@0 113 h1 *= 0x85ebca6b;
aoqi@0 114 h1 ^= h1 >> 13;
aoqi@0 115 h1 *= 0xc2b2ae35;
aoqi@0 116 h1 ^= h1 >> 16;
aoqi@0 117
aoqi@0 118 return h1;
aoqi@0 119 }
aoqi@0 120
aoqi@0 121 // Murmur3 hashing for Strings
aoqi@0 122 juint AltHashing::murmur3_32(juint seed, const jchar* data, int len) {
aoqi@0 123 juint h1 = seed;
aoqi@0 124
aoqi@0 125 int off = 0;
aoqi@0 126 int count = len;
aoqi@0 127
aoqi@0 128 // body
aoqi@0 129 while (count >= 2) {
aoqi@0 130 jchar d1 = data[off++] & 0xFFFF;
aoqi@0 131 jchar d2 = data[off++];
aoqi@0 132 juint k1 = (d1 | d2 << 16);
aoqi@0 133
aoqi@0 134 count -= 2;
aoqi@0 135
aoqi@0 136 k1 *= 0xcc9e2d51;
aoqi@0 137 k1 = Integer_rotateLeft(k1, 15);
aoqi@0 138 k1 *= 0x1b873593;
aoqi@0 139
aoqi@0 140 h1 ^= k1;
aoqi@0 141 h1 = Integer_rotateLeft(h1, 13);
aoqi@0 142 h1 = h1 * 5 + 0xe6546b64;
aoqi@0 143 }
aoqi@0 144
aoqi@0 145 // tail
aoqi@0 146
aoqi@0 147 if (count > 0) {
aoqi@0 148 juint k1 = (juint)data[off];
aoqi@0 149
aoqi@0 150 k1 *= 0xcc9e2d51;
aoqi@0 151 k1 = Integer_rotateLeft(k1, 15);
aoqi@0 152 k1 *= 0x1b873593;
aoqi@0 153 h1 ^= k1;
aoqi@0 154 }
aoqi@0 155
aoqi@0 156 // finalization
aoqi@0 157 h1 ^= len * 2; // (Character.SIZE / Byte.SIZE);
aoqi@0 158
aoqi@0 159 // finalization mix force all bits of a hash block to avalanche
aoqi@0 160 h1 ^= h1 >> 16;
aoqi@0 161 h1 *= 0x85ebca6b;
aoqi@0 162 h1 ^= h1 >> 13;
aoqi@0 163 h1 *= 0xc2b2ae35;
aoqi@0 164 h1 ^= h1 >> 16;
aoqi@0 165
aoqi@0 166 return h1;
aoqi@0 167 }
aoqi@0 168
aoqi@0 169 // Hash used for the seed.
aoqi@0 170 juint AltHashing::murmur3_32(juint seed, const int* data, int len) {
aoqi@0 171 juint h1 = seed;
aoqi@0 172
aoqi@0 173 int off = 0;
aoqi@0 174 int end = len;
aoqi@0 175
aoqi@0 176 // body
aoqi@0 177 while (off < end) {
aoqi@0 178 juint k1 = (juint)data[off++];
aoqi@0 179
aoqi@0 180 k1 *= 0xcc9e2d51;
aoqi@0 181 k1 = Integer_rotateLeft(k1, 15);
aoqi@0 182 k1 *= 0x1b873593;
aoqi@0 183
aoqi@0 184 h1 ^= k1;
aoqi@0 185 h1 = Integer_rotateLeft(h1, 13);
aoqi@0 186 h1 = h1 * 5 + 0xe6546b64;
aoqi@0 187 }
aoqi@0 188
aoqi@0 189 // tail (always empty, as body is always 32-bit chunks)
aoqi@0 190
aoqi@0 191 // finalization
aoqi@0 192
aoqi@0 193 h1 ^= len * 4; // (Integer.SIZE / Byte.SIZE);
aoqi@0 194
aoqi@0 195 // finalization mix force all bits of a hash block to avalanche
aoqi@0 196 h1 ^= h1 >> 16;
aoqi@0 197 h1 *= 0x85ebca6b;
aoqi@0 198 h1 ^= h1 >> 13;
aoqi@0 199 h1 *= 0xc2b2ae35;
aoqi@0 200 h1 ^= h1 >> 16;
aoqi@0 201
aoqi@0 202 return h1;
aoqi@0 203 }
aoqi@0 204
aoqi@0 205 juint AltHashing::murmur3_32(const int* data, int len) {
aoqi@0 206 return murmur3_32(0, data, len);
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 #ifndef PRODUCT
aoqi@0 210 // Overloaded versions for internal test.
aoqi@0 211 juint AltHashing::murmur3_32(const jbyte* data, int len) {
aoqi@0 212 return murmur3_32(0, data, len);
aoqi@0 213 }
aoqi@0 214
aoqi@0 215 juint AltHashing::murmur3_32(const jchar* data, int len) {
aoqi@0 216 return murmur3_32(0, data, len);
aoqi@0 217 }
aoqi@0 218
aoqi@0 219 // Internal test for alternate hashing. Translated from JDK version
aoqi@0 220 // test/sun/misc/Hashing.java
aoqi@0 221 static const jbyte ONE_BYTE[] = { (jbyte) 0x80};
aoqi@0 222 static const jbyte TWO_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81};
aoqi@0 223 static const jchar ONE_CHAR[] = { (jchar) 0x8180};
aoqi@0 224 static const jbyte THREE_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82};
aoqi@0 225 static const jbyte FOUR_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82, (jbyte) 0x83};
aoqi@0 226 static const jchar TWO_CHAR[] = { (jchar) 0x8180, (jchar) 0x8382};
aoqi@0 227 static const jint ONE_INT[] = { 0x83828180};
aoqi@0 228 static const jbyte SIX_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82, (jbyte) 0x83, (jbyte) 0x84, (jbyte) 0x85};
aoqi@0 229 static const jchar THREE_CHAR[] = { (jchar) 0x8180, (jchar) 0x8382, (jchar) 0x8584};
aoqi@0 230 static const jbyte EIGHT_BYTE[] = {
aoqi@0 231 (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82,
aoqi@0 232 (jbyte) 0x83, (jbyte) 0x84, (jbyte) 0x85,
aoqi@0 233 (jbyte) 0x86, (jbyte) 0x87};
aoqi@0 234 static const jchar FOUR_CHAR[] = {
aoqi@0 235 (jchar) 0x8180, (jchar) 0x8382,
aoqi@0 236 (jchar) 0x8584, (jchar) 0x8786};
aoqi@0 237
aoqi@0 238 static const jint TWO_INT[] = { 0x83828180, 0x87868584};
aoqi@0 239
aoqi@0 240 static const juint MURMUR3_32_X86_CHECK_VALUE = 0xB0F57EE3;
aoqi@0 241
aoqi@0 242 void AltHashing::testMurmur3_32_ByteArray() {
aoqi@0 243 // printf("testMurmur3_32_ByteArray\n");
aoqi@0 244
aoqi@0 245 jbyte vector[256];
aoqi@0 246 jbyte hashes[4 * 256];
aoqi@0 247
aoqi@0 248 for (int i = 0; i < 256; i++) {
aoqi@0 249 vector[i] = (jbyte) i;
aoqi@0 250 }
aoqi@0 251
aoqi@0 252 // Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
aoqi@0 253 for (int i = 0; i < 256; i++) {
aoqi@0 254 juint hash = murmur3_32(256 - i, vector, i);
aoqi@0 255 hashes[i * 4] = (jbyte) hash;
aoqi@0 256 hashes[i * 4 + 1] = (jbyte)(hash >> 8);
aoqi@0 257 hashes[i * 4 + 2] = (jbyte)(hash >> 16);
aoqi@0 258 hashes[i * 4 + 3] = (jbyte)(hash >> 24);
aoqi@0 259 }
aoqi@0 260
aoqi@0 261 // hash to get const result.
aoqi@0 262 juint final_hash = murmur3_32(hashes, 4*256);
aoqi@0 263
aoqi@0 264 assert (MURMUR3_32_X86_CHECK_VALUE == final_hash,
aoqi@0 265 err_msg(
aoqi@0 266 "Calculated hash result not as expected. Expected %08X got %08X\n",
aoqi@0 267 MURMUR3_32_X86_CHECK_VALUE,
aoqi@0 268 final_hash));
aoqi@0 269 }
aoqi@0 270
aoqi@0 271 void AltHashing::testEquivalentHashes() {
aoqi@0 272 juint jbytes, jchars, ints;
aoqi@0 273
aoqi@0 274 // printf("testEquivalentHashes\n");
aoqi@0 275
aoqi@0 276 jbytes = murmur3_32(TWO_BYTE, 2);
aoqi@0 277 jchars = murmur3_32(ONE_CHAR, 1);
aoqi@0 278 assert (jbytes == jchars,
aoqi@0 279 err_msg("Hashes did not match. b:%08x != c:%08x\n", jbytes, jchars));
aoqi@0 280
aoqi@0 281 jbytes = murmur3_32(FOUR_BYTE, 4);
aoqi@0 282 jchars = murmur3_32(TWO_CHAR, 2);
aoqi@0 283 ints = murmur3_32(ONE_INT, 1);
aoqi@0 284 assert ((jbytes == jchars) && (jbytes == ints),
aoqi@0 285 err_msg("Hashes did not match. b:%08x != c:%08x != i:%08x\n", jbytes, jchars, ints));
aoqi@0 286
aoqi@0 287 jbytes = murmur3_32(SIX_BYTE, 6);
aoqi@0 288 jchars = murmur3_32(THREE_CHAR, 3);
aoqi@0 289 assert (jbytes == jchars,
aoqi@0 290 err_msg("Hashes did not match. b:%08x != c:%08x\n", jbytes, jchars));
aoqi@0 291
aoqi@0 292 jbytes = murmur3_32(EIGHT_BYTE, 8);
aoqi@0 293 jchars = murmur3_32(FOUR_CHAR, 4);
aoqi@0 294 ints = murmur3_32(TWO_INT, 2);
aoqi@0 295 assert ((jbytes == jchars) && (jbytes == ints),
aoqi@0 296 err_msg("Hashes did not match. b:%08x != c:%08x != i:%08x\n", jbytes, jchars, ints));
aoqi@0 297 }
aoqi@0 298
aoqi@0 299 // Returns true if the alternate hashcode is correct
aoqi@0 300 void AltHashing::test_alt_hash() {
aoqi@0 301 testMurmur3_32_ByteArray();
aoqi@0 302 testEquivalentHashes();
aoqi@0 303 }
aoqi@0 304 #endif // PRODUCT

mercurial