coleenp@3865: /* minqi@5103: * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved. coleenp@3865: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. coleenp@3865: * coleenp@3865: * This code is free software; you can redistribute it and/or modify it coleenp@3865: * under the terms of the GNU General Public License version 2 only, as coleenp@3865: * published by the Free Software Foundation. coleenp@3865: * coleenp@3865: * This code is distributed in the hope that it will be useful, but WITHOUT coleenp@3865: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or coleenp@3865: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License coleenp@3865: * version 2 for more details (a copy is included in the LICENSE file that coleenp@3865: * accompanied this code). coleenp@3865: * coleenp@3865: * You should have received a copy of the GNU General Public License version coleenp@3865: * 2 along with this work; if not, write to the Free Software Foundation, coleenp@3865: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. coleenp@3865: * coleenp@3865: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA coleenp@3865: * or visit www.oracle.com if you need additional information or have any coleenp@3865: * questions. coleenp@3865: * coleenp@3865: */ coleenp@3865: coleenp@3865: #include "precompiled.hpp" coleenp@3865: #include "classfile/altHashing.hpp" coleenp@3865: #include "classfile/symbolTable.hpp" coleenp@3865: #include "classfile/systemDictionary.hpp" coleenp@3865: #include "oops/markOop.hpp" coleenp@3865: #include "runtime/thread.hpp" coleenp@3865: coleenp@3865: // Get the hash code of the classes mirror if it exists, otherwise just coleenp@3865: // return a random number, which is one of the possible hash code used for coleenp@3865: // objects. We don't want to call the synchronizer hash code to install coleenp@3865: // this value because it may safepoint. coleenp@4037: intptr_t object_hash(Klass* k) { coleenp@3865: intptr_t hc = k->java_mirror()->mark()->hash(); coleenp@3865: return hc != markOopDesc::no_hash ? hc : os::random(); coleenp@3865: } coleenp@3865: coleenp@3865: // Seed value used for each alternative hash calculated. coleenp@3865: jint AltHashing::compute_seed() { coleenp@3865: jlong nanos = os::javaTimeNanos(); coleenp@3865: jlong now = os::javaTimeMillis(); coleenp@3865: jint SEED_MATERIAL[8] = { coleenp@3865: (jint) object_hash(SystemDictionary::String_klass()), coleenp@3865: (jint) object_hash(SystemDictionary::System_klass()), coleenp@3865: (jint) os::random(), // current thread isn't a java thread coleenp@3865: (jint) (((julong)nanos) >> 32), coleenp@3865: (jint) nanos, coleenp@3865: (jint) (((julong)now) >> 32), coleenp@3865: (jint) now, coleenp@3865: (jint) (os::javaTimeNanos() >> 2) coleenp@3865: }; coleenp@3865: coleenp@3865: return murmur3_32(SEED_MATERIAL, 8); coleenp@3865: } coleenp@3865: coleenp@3865: coleenp@3865: // Murmur3 hashing for Symbol coleenp@3865: jint AltHashing::murmur3_32(jint seed, const jbyte* data, int len) { coleenp@3865: jint h1 = seed; coleenp@3865: int count = len; coleenp@3865: int offset = 0; coleenp@3865: coleenp@3865: // body coleenp@3865: while (count >= 4) { coleenp@3865: jint k1 = (data[offset] & 0x0FF) coleenp@3865: | (data[offset + 1] & 0x0FF) << 8 coleenp@3865: | (data[offset + 2] & 0x0FF) << 16 coleenp@3865: | data[offset + 3] << 24; coleenp@3865: coleenp@3865: count -= 4; coleenp@3865: offset += 4; coleenp@3865: coleenp@3865: k1 *= 0xcc9e2d51; coleenp@3865: k1 = Integer_rotateLeft(k1, 15); coleenp@3865: k1 *= 0x1b873593; coleenp@3865: coleenp@3865: h1 ^= k1; coleenp@3865: h1 = Integer_rotateLeft(h1, 13); coleenp@3865: h1 = h1 * 5 + 0xe6546b64; coleenp@3865: } coleenp@3865: coleenp@3865: // tail coleenp@3865: coleenp@3865: if (count > 0) { coleenp@3865: jint k1 = 0; coleenp@3865: coleenp@3865: switch (count) { coleenp@3865: case 3: coleenp@3865: k1 ^= (data[offset + 2] & 0xff) << 16; coleenp@3865: // fall through coleenp@3865: case 2: coleenp@3865: k1 ^= (data[offset + 1] & 0xff) << 8; coleenp@3865: // fall through coleenp@3865: case 1: coleenp@3865: k1 ^= (data[offset] & 0xff); coleenp@3865: // fall through coleenp@3865: default: coleenp@3865: k1 *= 0xcc9e2d51; coleenp@3865: k1 = Integer_rotateLeft(k1, 15); coleenp@3865: k1 *= 0x1b873593; coleenp@3865: h1 ^= k1; coleenp@3865: } coleenp@3865: } coleenp@3865: coleenp@3865: // finalization coleenp@3865: h1 ^= len; coleenp@3865: coleenp@3865: // finalization mix force all bits of a hash block to avalanche coleenp@3865: h1 ^= ((unsigned int)h1) >> 16; coleenp@3865: h1 *= 0x85ebca6b; coleenp@3865: h1 ^= ((unsigned int)h1) >> 13; coleenp@3865: h1 *= 0xc2b2ae35; coleenp@3865: h1 ^= ((unsigned int)h1) >> 16; coleenp@3865: coleenp@3865: return h1; coleenp@3865: } coleenp@3865: coleenp@3865: // Murmur3 hashing for Strings coleenp@3865: jint AltHashing::murmur3_32(jint seed, const jchar* data, int len) { coleenp@3865: jint h1 = seed; coleenp@3865: coleenp@3865: int off = 0; coleenp@3865: int count = len; coleenp@3865: coleenp@3865: // body coleenp@3865: while (count >= 2) { coleenp@3865: jchar d1 = data[off++] & 0xFFFF; coleenp@3865: jchar d2 = data[off++]; coleenp@3865: jint k1 = (d1 | d2 << 16); coleenp@3865: coleenp@3865: count -= 2; coleenp@3865: coleenp@3865: k1 *= 0xcc9e2d51; coleenp@3865: k1 = Integer_rotateLeft(k1, 15); coleenp@3865: k1 *= 0x1b873593; coleenp@3865: coleenp@3865: h1 ^= k1; coleenp@3865: h1 = Integer_rotateLeft(h1, 13); coleenp@3865: h1 = h1 * 5 + 0xe6546b64; coleenp@3865: } coleenp@3865: coleenp@3865: // tail coleenp@3865: coleenp@3865: if (count > 0) { coleenp@3865: int k1 = data[off]; coleenp@3865: coleenp@3865: k1 *= 0xcc9e2d51; coleenp@3865: k1 = Integer_rotateLeft(k1, 15); coleenp@3865: k1 *= 0x1b873593; coleenp@3865: h1 ^= k1; coleenp@3865: } coleenp@3865: coleenp@3865: // finalization coleenp@3865: h1 ^= len * 2; // (Character.SIZE / Byte.SIZE); coleenp@3865: coleenp@3865: // finalization mix force all bits of a hash block to avalanche coleenp@3865: h1 ^= ((unsigned int)h1) >> 16; coleenp@3865: h1 *= 0x85ebca6b; coleenp@3865: h1 ^= ((unsigned int)h1) >> 13; coleenp@3865: h1 *= 0xc2b2ae35; coleenp@3865: h1 ^= ((unsigned int)h1) >> 16; coleenp@3865: coleenp@3865: return h1; coleenp@3865: } coleenp@3865: coleenp@3865: // Hash used for the seed. coleenp@3865: jint AltHashing::murmur3_32(jint seed, const int* data, int len) { coleenp@3865: jint h1 = seed; coleenp@3865: coleenp@3865: int off = 0; coleenp@3865: int end = len; coleenp@3865: coleenp@3865: // body coleenp@3865: while (off < end) { coleenp@3865: jint k1 = data[off++]; coleenp@3865: coleenp@3865: k1 *= 0xcc9e2d51; coleenp@3865: k1 = Integer_rotateLeft(k1, 15); coleenp@3865: k1 *= 0x1b873593; coleenp@3865: coleenp@3865: h1 ^= k1; coleenp@3865: h1 = Integer_rotateLeft(h1, 13); coleenp@3865: h1 = h1 * 5 + 0xe6546b64; coleenp@3865: } coleenp@3865: coleenp@3865: // tail (always empty, as body is always 32-bit chunks) coleenp@3865: coleenp@3865: // finalization coleenp@3865: coleenp@3865: h1 ^= len * 4; // (Integer.SIZE / Byte.SIZE); coleenp@3865: coleenp@3865: // finalization mix force all bits of a hash block to avalanche coleenp@3865: h1 ^= ((juint)h1) >> 16; coleenp@3865: h1 *= 0x85ebca6b; coleenp@3865: h1 ^= ((juint)h1) >> 13; coleenp@3865: h1 *= 0xc2b2ae35; coleenp@3865: h1 ^= ((juint)h1) >> 16; coleenp@3865: coleenp@3865: return h1; coleenp@3865: } coleenp@3865: coleenp@3865: jint AltHashing::murmur3_32(const int* data, int len) { coleenp@3865: return murmur3_32(0, data, len); coleenp@3865: } coleenp@3865: coleenp@3865: #ifndef PRODUCT coleenp@3865: // Overloaded versions for internal test. coleenp@3865: jint AltHashing::murmur3_32(const jbyte* data, int len) { coleenp@3865: return murmur3_32(0, data, len); coleenp@3865: } coleenp@3865: coleenp@3865: jint AltHashing::murmur3_32(const jchar* data, int len) { coleenp@3865: return murmur3_32(0, data, len); coleenp@3865: } coleenp@3865: coleenp@3865: // Internal test for alternate hashing. Translated from JDK version coleenp@3865: // test/sun/misc/Hashing.java coleenp@3865: static const jbyte ONE_BYTE[] = { (jbyte) 0x80}; coleenp@3865: static const jbyte TWO_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81}; coleenp@3865: static const jchar ONE_CHAR[] = { (jchar) 0x8180}; coleenp@3865: static const jbyte THREE_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82}; coleenp@3865: static const jbyte FOUR_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82, (jbyte) 0x83}; coleenp@3865: static const jchar TWO_CHAR[] = { (jchar) 0x8180, (jchar) 0x8382}; coleenp@3865: static const jint ONE_INT[] = { 0x83828180}; coleenp@3865: static const jbyte SIX_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82, (jbyte) 0x83, (jbyte) 0x84, (jbyte) 0x85}; coleenp@3865: static const jchar THREE_CHAR[] = { (jchar) 0x8180, (jchar) 0x8382, (jchar) 0x8584}; coleenp@3865: static const jbyte EIGHT_BYTE[] = { coleenp@3865: (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82, coleenp@3865: (jbyte) 0x83, (jbyte) 0x84, (jbyte) 0x85, coleenp@3865: (jbyte) 0x86, (jbyte) 0x87}; coleenp@3865: static const jchar FOUR_CHAR[] = { coleenp@3865: (jchar) 0x8180, (jchar) 0x8382, coleenp@3865: (jchar) 0x8584, (jchar) 0x8786}; coleenp@3865: coleenp@3865: static const jint TWO_INT[] = { 0x83828180, 0x87868584}; coleenp@3865: coleenp@3865: static const juint MURMUR3_32_X86_CHECK_VALUE = 0xB0F57EE3; coleenp@3865: coleenp@3865: void AltHashing::testMurmur3_32_ByteArray() { coleenp@3865: // printf("testMurmur3_32_ByteArray\n"); coleenp@3865: minqi@5103: jbyte vector[256]; minqi@5103: jbyte hashes[4 * 256]; coleenp@3865: coleenp@3865: for (int i = 0; i < 256; i++) { coleenp@3865: vector[i] = (jbyte) i; coleenp@3865: } coleenp@3865: coleenp@3865: // Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255} coleenp@3865: for (int i = 0; i < 256; i++) { coleenp@3865: jint hash = murmur3_32(256 - i, vector, i); coleenp@3865: hashes[i * 4] = (jbyte) hash; coleenp@3865: hashes[i * 4 + 1] = (jbyte) (((juint)hash) >> 8); coleenp@3865: hashes[i * 4 + 2] = (jbyte) (((juint)hash) >> 16); coleenp@3865: hashes[i * 4 + 3] = (jbyte) (((juint)hash) >> 24); coleenp@3865: } coleenp@3865: coleenp@3865: // hash to get const result. coleenp@3865: juint final_hash = murmur3_32(hashes, 4*256); coleenp@3865: coleenp@3865: assert (MURMUR3_32_X86_CHECK_VALUE == final_hash, coleenp@3865: err_msg( coleenp@3865: "Calculated hash result not as expected. Expected %08X got %08X\n", coleenp@3865: MURMUR3_32_X86_CHECK_VALUE, coleenp@3865: final_hash)); coleenp@3865: } coleenp@3865: coleenp@3865: void AltHashing::testEquivalentHashes() { coleenp@3865: jint jbytes, jchars, ints; coleenp@3865: coleenp@3865: // printf("testEquivalentHashes\n"); coleenp@3865: coleenp@3865: jbytes = murmur3_32(TWO_BYTE, 2); coleenp@3865: jchars = murmur3_32(ONE_CHAR, 1); coleenp@3865: assert (jbytes == jchars, coleenp@3865: err_msg("Hashes did not match. b:%08x != c:%08x\n", jbytes, jchars)); coleenp@3865: coleenp@3865: jbytes = murmur3_32(FOUR_BYTE, 4); coleenp@3865: jchars = murmur3_32(TWO_CHAR, 2); coleenp@3865: ints = murmur3_32(ONE_INT, 1); coleenp@3865: assert ((jbytes == jchars) && (jbytes == ints), coleenp@3865: err_msg("Hashes did not match. b:%08x != c:%08x != i:%08x\n", jbytes, jchars, ints)); coleenp@3865: coleenp@3865: jbytes = murmur3_32(SIX_BYTE, 6); coleenp@3865: jchars = murmur3_32(THREE_CHAR, 3); coleenp@3865: assert (jbytes == jchars, coleenp@3865: err_msg("Hashes did not match. b:%08x != c:%08x\n", jbytes, jchars)); coleenp@3865: coleenp@3865: jbytes = murmur3_32(EIGHT_BYTE, 8); coleenp@3865: jchars = murmur3_32(FOUR_CHAR, 4); coleenp@3865: ints = murmur3_32(TWO_INT, 2); coleenp@3865: assert ((jbytes == jchars) && (jbytes == ints), coleenp@3865: err_msg("Hashes did not match. b:%08x != c:%08x != i:%08x\n", jbytes, jchars, ints)); coleenp@3865: } coleenp@3865: coleenp@3865: // Returns true if the alternate hashcode is correct coleenp@3865: void AltHashing::test_alt_hash() { coleenp@3865: testMurmur3_32_ByteArray(); coleenp@3865: testEquivalentHashes(); coleenp@3865: } coleenp@3865: #endif // PRODUCT