Java Code Examples for sun.misc.Unsafe#ARRAY_LONG_BASE_OFFSET

The following examples show how to use sun.misc.Unsafe#ARRAY_LONG_BASE_OFFSET . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ConcurrentMonoidLongTable.java    From metrics with Apache License 2.0 6 votes vote down vote up
@Override
public boolean next() {
  i++;
  if (i >= tagSets.length || tagSets[i] == null) {
    return false;
  }

  long index = index(tagSets[i], true);

  if (NOT_FOUND == index) {
    LOGGER.error("Missing index on Read. Tags: {}. Concurrency error or bug",
        Arrays.asList(tagSets[i]));
    return false;
  }

  // Decode table index and slot index from long.
  // Upper 32 bits represent the table index and lower 32 bits represent the slot index.
  // This logic is replicated in multiple places for performance reasons.
  int tableIndex = (int) ((index & TABLE_MASK) >> 32);
  int slotIndex = (int) (index & SLOT_MASK);

  table = tables.get(tableIndex);
  base = Unsafe.ARRAY_LONG_BASE_OFFSET + slotIndex * Unsafe.ARRAY_LONG_INDEX_SCALE;
  return true;
}
 
Example 2
Source File: RenderBuffer.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(long[] x, int offset, int length) {
    // assert (position() % SIZEOF_LONG == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_LONG;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putLong(x[i]);
        }
    }
    return this;
}
 
Example 3
Source File: RenderBuffer.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(long[] x, int offset, int length) {
    // assert (position() % SIZEOF_LONG == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_LONG;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putLong(x[i]);
        }
    }
    return this;
}
 
Example 4
Source File: RenderBuffer.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(long[] x, int offset, int length) {
    // assert (position() % SIZEOF_LONG == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_LONG;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putLong(x[i]);
        }
    }
    return this;
}
 
Example 5
Source File: RenderBuffer.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(long[] x, int offset, int length) {
    // assert (position() % SIZEOF_LONG == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_LONG;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putLong(x[i]);
        }
    }
    return this;
}
 
Example 6
Source File: SegmentedLongArray.java    From hollow with Apache License 2.0 5 votes vote down vote up
public void fill(long value) {
    for(int i=0;i<segments.length;i++) {
        long offset = Unsafe.ARRAY_LONG_BASE_OFFSET;
        for(int j=0;j<segments[i].length;j++) {
            unsafe.putOrderedLong(segments[i], offset, value);
            offset += 8;
        }
    }
}
 
Example 7
Source File: FixedLengthElementArrayPlainPut.java    From hollow with Apache License 2.0 5 votes vote down vote up
public long getElementValue(long index, int bitsPerElement, long mask) {
    long whichByte = index >>> 3;
    int whichBit = (int) (index & 0x07);

    int whichSegment = (int) (whichByte >>> log2OfSegmentSizeInBytes);

    long[] segment = segments[whichSegment];
    long elementByteOffset = (long) Unsafe.ARRAY_LONG_BASE_OFFSET + (whichByte & byteBitmask);
    long l = unsafe.getLong(segment, elementByteOffset) >>> whichBit;

    return l & mask;
}
 
Example 8
Source File: SegmentedLongArrayPlainPut.java    From hollow with Apache License 2.0 5 votes vote down vote up
public void fill(long value) {
    for (int i = 0; i < segments.length; i++) {
        long offset = Unsafe.ARRAY_LONG_BASE_OFFSET;
        for (int j = 0; j < segments[i].length; j++) {
            unsafe.putLong(segments[i], offset, value);
            offset += 8;
        }
    }
}
 
Example 9
Source File: RenderBuffer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(long[] x, int offset, int length) {
    // assert (position() % SIZEOF_LONG == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_LONG;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putLong(x[i]);
        }
    }
    return this;
}
 
Example 10
Source File: RenderBuffer.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(long[] x, int offset, int length) {
    // assert (position() % SIZEOF_LONG == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_LONG;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putLong(x[i]);
        }
    }
    return this;
}
 
Example 11
Source File: RenderBuffer.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(long[] x, int offset, int length) {
    // assert (position() % SIZEOF_LONG == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_LONG;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putLong(x[i]);
        }
    }
    return this;
}
 
Example 12
Source File: RenderBuffer.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(long[] x, int offset, int length) {
    // assert (position() % SIZEOF_LONG == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_LONG;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putLong(x[i]);
        }
    }
    return this;
}
 
Example 13
Source File: RenderBuffer.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(long[] x, int offset, int length) {
    // assert (position() % SIZEOF_LONG == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_LONG;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putLong(x[i]);
        }
    }
    return this;
}
 
Example 14
Source File: RenderBuffer.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(long[] x, int offset, int length) {
    // assert (position() % SIZEOF_LONG == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_LONG;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putLong(x[i]);
        }
    }
    return this;
}
 
Example 15
Source File: RenderBuffer.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public RenderBuffer put(long[] x, int offset, int length) {
    // assert (position() % SIZEOF_LONG == 0);
    if (length > COPY_FROM_ARRAY_THRESHOLD) {
        long offsetInBytes = offset * SIZEOF_LONG + Unsafe.ARRAY_LONG_BASE_OFFSET;
        long lengthInBytes = length * SIZEOF_LONG;
        unsafe.copyMemory(x, offsetInBytes, null, curAddress, lengthInBytes);
        position(position() + lengthInBytes);
    } else {
        int end = offset + length;
        for (int i = offset; i < end; i++) {
            putLong(x[i]);
        }
    }
    return this;
}
 
Example 16
Source File: RawHashTable.java    From metrics with Apache License 2.0 5 votes vote down vote up
private long getLong(final String[] tags, int offset) {
  final long hashCode = Arrays.hashCode(tags);
  final int i = index(hashCode);
  final long base = Unsafe.ARRAY_LONG_BASE_OFFSET + i * Unsafe.ARRAY_LONG_INDEX_SCALE;

  return unsafe.getLongVolatile(table, base + offset * Unsafe.ARRAY_LONG_INDEX_SCALE);
}
 
Example 17
Source File: VirtualArrayNode.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public static int entryIndexForOffset(long constantOffset, JavaKind expectedEntryKind, ResolvedJavaType componentType, int length) {
    int baseOffset;
    int indexScale;
    switch (componentType.getJavaKind()) {
        case Boolean:
            baseOffset = Unsafe.ARRAY_BOOLEAN_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_BOOLEAN_INDEX_SCALE;
            break;
        case Byte:
            baseOffset = Unsafe.ARRAY_BYTE_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_BYTE_INDEX_SCALE;
            break;
        case Short:
            baseOffset = Unsafe.ARRAY_SHORT_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_SHORT_INDEX_SCALE;
            break;
        case Char:
            baseOffset = Unsafe.ARRAY_CHAR_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_CHAR_INDEX_SCALE;
            break;
        case Int:
            baseOffset = Unsafe.ARRAY_INT_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_INT_INDEX_SCALE;
            break;
        case Long:
            baseOffset = Unsafe.ARRAY_LONG_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_LONG_INDEX_SCALE;
            break;
        case Float:
            baseOffset = Unsafe.ARRAY_FLOAT_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_FLOAT_INDEX_SCALE;
            break;
        case Double:
            baseOffset = Unsafe.ARRAY_DOUBLE_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_DOUBLE_INDEX_SCALE;
            break;
        case Object:
            baseOffset = Unsafe.ARRAY_OBJECT_BASE_OFFSET;
            indexScale = Unsafe.ARRAY_OBJECT_INDEX_SCALE;
            break;
        default:
            return -1;
    }
    long offset;
    if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN && componentType.isPrimitive()) {
        // On big endian, we do just get expect the type be right aligned in this memory slot
        offset = constantOffset - (componentType.getJavaKind().getByteCount() - Math.min(componentType.getJavaKind().getByteCount(), 4 + expectedEntryKind.getByteCount()));
    } else {
        offset = constantOffset;
    }
    long index = offset - baseOffset;
    if (index % indexScale != 0) {
        return -1;
    }
    long elementIndex = index / indexScale;
    if (elementIndex < 0 || elementIndex >= length) {
        return -1;
    }
    return (int) elementIndex;
}
 
Example 18
Source File: ConcurrentMonoidLongTable.java    From metrics with Apache License 2.0 4 votes vote down vote up
/**
 * Find index of the record for the given key in the linear probing table.
 *
 * <p>When a slot in the table is taken, it will never be released nor changed.</p>
 *
 * @param tags key to use for table
 * @return index position in the table for the record. The 64 bit long value has two int values
 * encoded into it. The higher 32 bits represent the table index and the lower 32 bits represent
 * the slot index. And returns {@link #NOT_FOUND} if record not found.
 */
long index(final String[] tags, final boolean isReading) {
  final long key = hashCode(tags);
  for (int tableIndex = 0; tableIndex < tables.size(); tableIndex++) {
    long[] table = tables.get(tableIndex);
    AtomicInteger recordCount = recordCounts.get(tableIndex);
    int tableCapacity = tableCapacities.get(tableIndex);
    final int slot = getSlot(key, table.length / recordSize);
    final int startIndex = slot * recordSize;
    int slotIndex = startIndex;
    for (; ; ) {
      long offset = Unsafe.ARRAY_LONG_BASE_OFFSET + slotIndex * Unsafe.ARRAY_LONG_INDEX_SCALE;
      long candidate = unsafe.getLongVolatile(table, offset);

      // check if we found our key
      if (key == candidate) {
        // Encode table index and slot index into a long.
        // Upper 32 bits represent the table index and lower 32 bits represent the slot index.
        // This logic is replicated in multiple places for performance reasons.
        return ((long) tableIndex) << 32 | ((long) slotIndex);
      }

      boolean emptySlot = 0L == candidate;

      if (emptySlot) {

        if (isReading) {
          break; // If the slot is empty while reading, skip to the next table.
        } else if (recordCount.get() >= tableCapacity) {
          break; // we're writing but the table is 70% full
        } else {
          ///CLOVER:OFF
          // No reliable way to test without being able to mock unsafe
          if (unsafe.compareAndSwapLong(table, offset, 0L, key)) { // try to reserve it
            ///CLOVER:ON

            //increment the record count
            recordCount.incrementAndGet();

            // reset update timestamp
            unsafe.putLongVolatile(table, offset + Unsafe.ARRAY_LONG_INDEX_SCALE, 0L);
            // It is ok if we lose some data from other threads while writing identity
            for (int j = 0; j < identity.length; j++) {
              unsafe.putLongVolatile(table,
                  offset + (RESERVED_FIELDS + j) * Unsafe.ARRAY_LONG_INDEX_SCALE, identity[j]);
            }

            //increment the total size;
            int tagIndex = unsafe.getAndAddInt(this, usedOffset, 1);
            if (tagIndex >= tagSets.length) {
              // grow tag set
              synchronized (this) {
                if (tagIndex >= tagSets.length) {
                  final int oldLength = tagSets.length;
                  final int newLength =
                      oldLength > TAGSETS_MAX_INCREMENT ? oldLength + TAGSETS_MAX_INCREMENT
                          : oldLength * 2;
                  tagSets = Arrays.copyOf(tagSets, newLength);
                }
              }
            }

            // Store tags in the tag array for iteration purposes only
            tagSets[tagIndex] = Arrays.copyOf(tags, tags.length);

            // Encode table index and slot index into a long.
            // Upper 32 bits represent the table index and lower 32 bits represent the slot index.
            // This logic is replicated in multiple places for performance reasons.
            return ((long) tableIndex) << 32 | ((long) slotIndex);
          }
        }
      } else {
        slotIndex += recordSize;
        if (slotIndex >= table.length) {
          slotIndex = 0;
        }
        if (slotIndex == startIndex) {
          break; // check the next table
        }
      }
    }
  }
  if (isReading) {
    return NOT_FOUND;
  } else {
    if (growTable()) {
      return index(tags, isReading);
    } else {
      return NOT_FOUND;
    }
  }
}
 
Example 19
Source File: RawHashTable.java    From metrics with Apache License 2.0 4 votes vote down vote up
int index(final long key) {
  int start = (Math.abs((int) key) % capacity);
  int i = start < 0 ? 0 : start * RECORD_SIZE;
  boolean failSafe = false;

  for (int counter = 1; ; counter++) {

    final long offset = Unsafe.ARRAY_LONG_BASE_OFFSET + i * Unsafe.ARRAY_LONG_INDEX_SCALE;

    // check if we found our key
    final long candidate = unsafe.getLongVolatile(table, offset);
    if (key == candidate) {
      unsafe.putIntVolatile(this, scanLengthOffset, counter);
      return i;
    }

    // check if we found empty slot
    if (0L == candidate) {
      // try to reserve it

      ///CLOVER:OFF
      // No reliable way to test without being able to mock unsafe
      if (unsafe.compareAndSwapLong(table, offset, 0L, key)) {
        ///CLOVER:ON

        final int localUsed = unsafe.getAndAddInt(this, usedOffset, 1) + 1;
        unsafe.putLongVolatile(table, offset + 3 * Unsafe.ARRAY_LONG_INDEX_SCALE, Long.MAX_VALUE);
        unsafe.putLongVolatile(table, offset + 4 * Unsafe.ARRAY_LONG_INDEX_SCALE, Long.MIN_VALUE);

        unsafe.putIntVolatile(this, scanLengthOffset, counter);
        return i;
      }
    } else {
      // go to next record
      i += RECORD_SIZE;
      if (i >= table.length) {
        if (failSafe) {
          throw new IllegalStateException("No more space in linear probing table");
        } else {
          i = 0;
          failSafe = true;
        }
      }
    }
  }
}
 
Example 20
Source File: FixedLengthElementArray.java    From hollow with Apache License 2.0 3 votes vote down vote up
/**
 * Gets a masked element value, comprising of {@code bitsPerElement} bits, at the given
 * bit {@code index}.
 *
 * @param index the bit index
 * @param bitsPerElement bits per element, must be less than 61 otherwise
 * the result is undefined
 * @param mask the mask to apply to an element value before it is returned.
 * The mask should be less than or equal to {@code (1L << bitsPerElement) - 1} to
 * guarantee that one or more (possibly) partial element values occurring
 * before and after the desired element value are not included in the returned value.
 * @return the masked element value
 */
public long getElementValue(long index, int bitsPerElement, long mask) {
    long whichByte = index >>> 3;
    int whichBit = (int) (index & 0x07);

    int whichSegment = (int) (whichByte >>> log2OfSegmentSizeInBytes);

    long[] segment = segments[whichSegment];
    long elementByteOffset = (long)Unsafe.ARRAY_LONG_BASE_OFFSET + (whichByte & byteBitmask);
    long l = unsafe.getLong(segment, elementByteOffset) >>> whichBit;

    return l & mask;
}