src/share/vm/classfile/altHashing.cpp

changeset 3865
e9140bf80b4a
child 4037
da91efe96a93
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/classfile/altHashing.cpp	Wed Jun 13 19:52:59 2012 -0400
     1.3 @@ -0,0 +1,304 @@
     1.4 +/*
     1.5 + * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#include "precompiled.hpp"
    1.29 +#include "classfile/altHashing.hpp"
    1.30 +#include "classfile/symbolTable.hpp"
    1.31 +#include "classfile/systemDictionary.hpp"
    1.32 +#include "oops/markOop.hpp"
    1.33 +#include "runtime/thread.hpp"
    1.34 +
    1.35 +// Get the hash code of the classes mirror if it exists, otherwise just
    1.36 +// return a random number, which is one of the possible hash code used for
    1.37 +// objects.  We don't want to call the synchronizer hash code to install
    1.38 +// this value because it may safepoint.
    1.39 +intptr_t object_hash(klassOop k) {
    1.40 +  intptr_t hc = k->java_mirror()->mark()->hash();
    1.41 +  return hc != markOopDesc::no_hash ? hc : os::random();
    1.42 +}
    1.43 +
    1.44 +// Seed value used for each alternative hash calculated.
    1.45 +jint AltHashing::compute_seed() {
    1.46 +  jlong nanos = os::javaTimeNanos();
    1.47 +  jlong now = os::javaTimeMillis();
    1.48 +  jint SEED_MATERIAL[8] = {
    1.49 +            (jint) object_hash(SystemDictionary::String_klass()),
    1.50 +            (jint) object_hash(SystemDictionary::System_klass()),
    1.51 +            (jint) os::random(),  // current thread isn't a java thread
    1.52 +            (jint) (((julong)nanos) >> 32),
    1.53 +            (jint) nanos,
    1.54 +            (jint) (((julong)now) >> 32),
    1.55 +            (jint) now,
    1.56 +            (jint) (os::javaTimeNanos() >> 2)
    1.57 +  };
    1.58 +
    1.59 +  return murmur3_32(SEED_MATERIAL, 8);
    1.60 +}
    1.61 +
    1.62 +
    1.63 +// Murmur3 hashing for Symbol
    1.64 +jint AltHashing::murmur3_32(jint seed, const jbyte* data, int len) {
    1.65 +  jint h1 = seed;
    1.66 +  int count = len;
    1.67 +  int offset = 0;
    1.68 +
    1.69 +  // body
    1.70 +  while (count >= 4) {
    1.71 +    jint k1 = (data[offset] & 0x0FF)
    1.72 +        | (data[offset + 1] & 0x0FF) << 8
    1.73 +        | (data[offset + 2] & 0x0FF) << 16
    1.74 +        | data[offset + 3] << 24;
    1.75 +
    1.76 +    count -= 4;
    1.77 +    offset += 4;
    1.78 +
    1.79 +    k1 *= 0xcc9e2d51;
    1.80 +    k1 = Integer_rotateLeft(k1, 15);
    1.81 +    k1 *= 0x1b873593;
    1.82 +
    1.83 +    h1 ^= k1;
    1.84 +    h1 = Integer_rotateLeft(h1, 13);
    1.85 +    h1 = h1 * 5 + 0xe6546b64;
    1.86 +  }
    1.87 +
    1.88 +  // tail
    1.89 +
    1.90 +  if (count > 0) {
    1.91 +    jint k1 = 0;
    1.92 +
    1.93 +    switch (count) {
    1.94 +      case 3:
    1.95 +        k1 ^= (data[offset + 2] & 0xff) << 16;
    1.96 +      // fall through
    1.97 +      case 2:
    1.98 +        k1 ^= (data[offset + 1] & 0xff) << 8;
    1.99 +      // fall through
   1.100 +      case 1:
   1.101 +        k1 ^= (data[offset] & 0xff);
   1.102 +      // fall through
   1.103 +      default:
   1.104 +        k1 *= 0xcc9e2d51;
   1.105 +        k1 = Integer_rotateLeft(k1, 15);
   1.106 +        k1 *= 0x1b873593;
   1.107 +        h1 ^= k1;
   1.108 +    }
   1.109 +  }
   1.110 +
   1.111 +  // finalization
   1.112 +  h1 ^= len;
   1.113 +
   1.114 +  // finalization mix force all bits of a hash block to avalanche
   1.115 +  h1 ^= ((unsigned int)h1) >> 16;
   1.116 +  h1 *= 0x85ebca6b;
   1.117 +  h1 ^= ((unsigned int)h1) >> 13;
   1.118 +  h1 *= 0xc2b2ae35;
   1.119 +  h1 ^= ((unsigned int)h1) >> 16;
   1.120 +
   1.121 +  return h1;
   1.122 +}
   1.123 +
   1.124 +// Murmur3 hashing for Strings
   1.125 +jint AltHashing::murmur3_32(jint seed, const jchar* data, int len) {
   1.126 +  jint h1 = seed;
   1.127 +
   1.128 +  int off = 0;
   1.129 +  int count = len;
   1.130 +
   1.131 +  // body
   1.132 +  while (count >= 2) {
   1.133 +    jchar d1 = data[off++] & 0xFFFF;
   1.134 +    jchar d2 = data[off++];
   1.135 +    jint k1 = (d1 | d2 << 16);
   1.136 +
   1.137 +    count -= 2;
   1.138 +
   1.139 +    k1 *= 0xcc9e2d51;
   1.140 +    k1 = Integer_rotateLeft(k1, 15);
   1.141 +    k1 *= 0x1b873593;
   1.142 +
   1.143 +    h1 ^= k1;
   1.144 +    h1 = Integer_rotateLeft(h1, 13);
   1.145 +    h1 = h1 * 5 + 0xe6546b64;
   1.146 +  }
   1.147 +
   1.148 +  // tail
   1.149 +
   1.150 +  if (count > 0) {
   1.151 +    int k1 = data[off];
   1.152 +
   1.153 +    k1 *= 0xcc9e2d51;
   1.154 +    k1 = Integer_rotateLeft(k1, 15);
   1.155 +    k1 *= 0x1b873593;
   1.156 +    h1 ^= k1;
   1.157 +  }
   1.158 +
   1.159 +  // finalization
   1.160 +  h1 ^= len * 2; // (Character.SIZE / Byte.SIZE);
   1.161 +
   1.162 +  // finalization mix force all bits of a hash block to avalanche
   1.163 +  h1 ^= ((unsigned int)h1) >> 16;
   1.164 +  h1 *= 0x85ebca6b;
   1.165 +  h1 ^= ((unsigned int)h1) >> 13;
   1.166 +  h1 *= 0xc2b2ae35;
   1.167 +  h1 ^= ((unsigned int)h1) >> 16;
   1.168 +
   1.169 +  return h1;
   1.170 +}
   1.171 +
   1.172 +// Hash used for the seed.
   1.173 +jint AltHashing::murmur3_32(jint seed, const int* data, int len) {
   1.174 +  jint h1 = seed;
   1.175 +
   1.176 +  int off = 0;
   1.177 +  int end = len;
   1.178 +
   1.179 +  // body
   1.180 +  while (off < end) {
   1.181 +    jint k1 = data[off++];
   1.182 +
   1.183 +    k1 *= 0xcc9e2d51;
   1.184 +    k1 = Integer_rotateLeft(k1, 15);
   1.185 +    k1 *= 0x1b873593;
   1.186 +
   1.187 +    h1 ^= k1;
   1.188 +    h1 = Integer_rotateLeft(h1, 13);
   1.189 +    h1 = h1 * 5 + 0xe6546b64;
   1.190 +  }
   1.191 +
   1.192 +  // tail (always empty, as body is always 32-bit chunks)
   1.193 +
   1.194 +  // finalization
   1.195 +
   1.196 +  h1 ^= len * 4; // (Integer.SIZE / Byte.SIZE);
   1.197 +
   1.198 +  // finalization mix force all bits of a hash block to avalanche
   1.199 +  h1 ^= ((juint)h1) >> 16;
   1.200 +  h1 *= 0x85ebca6b;
   1.201 +  h1 ^= ((juint)h1) >> 13;
   1.202 +  h1 *= 0xc2b2ae35;
   1.203 +  h1 ^= ((juint)h1) >> 16;
   1.204 +
   1.205 +  return h1;
   1.206 +}
   1.207 +
   1.208 +jint AltHashing::murmur3_32(const int* data, int len) {
   1.209 +  return murmur3_32(0, data, len);
   1.210 +}
   1.211 +
   1.212 +#ifndef PRODUCT
   1.213 +// Overloaded versions for internal test.
   1.214 +jint AltHashing::murmur3_32(const jbyte* data, int len) {
   1.215 +  return murmur3_32(0, data, len);
   1.216 +}
   1.217 +
   1.218 +jint AltHashing::murmur3_32(const jchar* data, int len) {
   1.219 +  return murmur3_32(0, data, len);
   1.220 +}
   1.221 +
   1.222 +// Internal test for alternate hashing.  Translated from JDK version
   1.223 +// test/sun/misc/Hashing.java
   1.224 +static const jbyte ONE_BYTE[] = { (jbyte) 0x80};
   1.225 +static const jbyte TWO_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81};
   1.226 +static const jchar ONE_CHAR[] = { (jchar) 0x8180};
   1.227 +static const jbyte THREE_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82};
   1.228 +static const jbyte FOUR_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82, (jbyte) 0x83};
   1.229 +static const jchar TWO_CHAR[] = { (jchar) 0x8180, (jchar) 0x8382};
   1.230 +static const jint ONE_INT[] = { 0x83828180};
   1.231 +static const jbyte SIX_BYTE[] = { (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82, (jbyte) 0x83, (jbyte) 0x84, (jbyte) 0x85};
   1.232 +static const jchar THREE_CHAR[] = { (jchar) 0x8180, (jchar) 0x8382, (jchar) 0x8584};
   1.233 +static const jbyte EIGHT_BYTE[] = {
   1.234 +  (jbyte) 0x80, (jbyte) 0x81, (jbyte) 0x82,
   1.235 +  (jbyte) 0x83, (jbyte) 0x84, (jbyte) 0x85,
   1.236 +  (jbyte) 0x86, (jbyte) 0x87};
   1.237 +static const jchar FOUR_CHAR[] = {
   1.238 +  (jchar) 0x8180, (jchar) 0x8382,
   1.239 +  (jchar) 0x8584, (jchar) 0x8786};
   1.240 +
   1.241 +static const jint TWO_INT[] = { 0x83828180, 0x87868584};
   1.242 +
   1.243 +static const juint MURMUR3_32_X86_CHECK_VALUE = 0xB0F57EE3;
   1.244 +
   1.245 +void AltHashing::testMurmur3_32_ByteArray() {
   1.246 +  // printf("testMurmur3_32_ByteArray\n");
   1.247 +
   1.248 +  jbyte* vector = new jbyte[256];
   1.249 +  jbyte* hashes = new jbyte[4 * 256];
   1.250 +
   1.251 +  for (int i = 0; i < 256; i++) {
   1.252 +    vector[i] = (jbyte) i;
   1.253 +  }
   1.254 +
   1.255 +  // Hash subranges {}, {0}, {0,1}, {0,1,2}, ..., {0,...,255}
   1.256 +  for (int i = 0; i < 256; i++) {
   1.257 +    jint hash = murmur3_32(256 - i, vector, i);
   1.258 +    hashes[i * 4] = (jbyte) hash;
   1.259 +    hashes[i * 4 + 1] = (jbyte) (((juint)hash) >> 8);
   1.260 +    hashes[i * 4 + 2] = (jbyte) (((juint)hash) >> 16);
   1.261 +    hashes[i * 4 + 3] = (jbyte) (((juint)hash) >> 24);
   1.262 +  }
   1.263 +
   1.264 +  // hash to get const result.
   1.265 +  juint final_hash = murmur3_32(hashes, 4*256);
   1.266 +
   1.267 +  assert (MURMUR3_32_X86_CHECK_VALUE == final_hash,
   1.268 +    err_msg(
   1.269 +        "Calculated hash result not as expected. Expected %08X got %08X\n",
   1.270 +        MURMUR3_32_X86_CHECK_VALUE,
   1.271 +        final_hash));
   1.272 +}
   1.273 +
   1.274 +void AltHashing::testEquivalentHashes() {
   1.275 +  jint jbytes, jchars, ints;
   1.276 +
   1.277 +  // printf("testEquivalentHashes\n");
   1.278 +
   1.279 +  jbytes = murmur3_32(TWO_BYTE, 2);
   1.280 +  jchars = murmur3_32(ONE_CHAR, 1);
   1.281 +  assert (jbytes == jchars,
   1.282 +    err_msg("Hashes did not match. b:%08x != c:%08x\n", jbytes, jchars));
   1.283 +
   1.284 +  jbytes = murmur3_32(FOUR_BYTE, 4);
   1.285 +  jchars = murmur3_32(TWO_CHAR, 2);
   1.286 +  ints = murmur3_32(ONE_INT, 1);
   1.287 +  assert ((jbytes == jchars) && (jbytes == ints),
   1.288 +    err_msg("Hashes did not match. b:%08x != c:%08x != i:%08x\n", jbytes, jchars, ints));
   1.289 +
   1.290 +  jbytes = murmur3_32(SIX_BYTE, 6);
   1.291 +  jchars = murmur3_32(THREE_CHAR, 3);
   1.292 +  assert (jbytes == jchars,
   1.293 +    err_msg("Hashes did not match. b:%08x != c:%08x\n", jbytes, jchars));
   1.294 +
   1.295 +  jbytes = murmur3_32(EIGHT_BYTE, 8);
   1.296 +  jchars = murmur3_32(FOUR_CHAR, 4);
   1.297 +  ints = murmur3_32(TWO_INT, 2);
   1.298 +  assert ((jbytes == jchars) && (jbytes == ints),
   1.299 +    err_msg("Hashes did not match. b:%08x != c:%08x != i:%08x\n", jbytes, jchars, ints));
   1.300 +}
   1.301 +
   1.302 +// Returns true if the alternate hashcode is correct
   1.303 +void AltHashing::test_alt_hash() {
   1.304 +  testMurmur3_32_ByteArray();
   1.305 +  testEquivalentHashes();
   1.306 +}
   1.307 +#endif // PRODUCT

mercurial