Java Code Examples for org.agrona.concurrent.AtomicBuffer#getIntVolatile()

The following examples show how to use org.agrona.concurrent.AtomicBuffer#getIntVolatile() . 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: BufferAlignmentAgentTest.java    From agrona with Apache License 2.0 6 votes vote down vote up
private void testAlignedAtomicMethods(final AtomicBuffer buffer, final int offset)
{
    buffer.getLongVolatile(offset + SIZE_OF_LONG);
    buffer.putLongVolatile(offset + SIZE_OF_LONG, Long.MAX_VALUE);
    buffer.compareAndSetLong(offset + SIZE_OF_LONG, Long.MAX_VALUE, Long.MAX_VALUE);
    buffer.getAndAddLong(offset + SIZE_OF_LONG, Long.MAX_VALUE);
    buffer.getAndSetLong(offset + SIZE_OF_LONG, Long.MAX_VALUE);
    buffer.putLongOrdered(offset + SIZE_OF_LONG, Long.MAX_VALUE);
    buffer.addLongOrdered(offset + SIZE_OF_LONG, Long.MAX_VALUE);

    buffer.getIntVolatile(offset + SIZE_OF_INT);
    buffer.putIntVolatile(offset + SIZE_OF_INT, Integer.MAX_VALUE);
    buffer.compareAndSetInt(offset + SIZE_OF_INT, Integer.MAX_VALUE, Integer.MAX_VALUE);
    buffer.getAndAddInt(offset + SIZE_OF_INT, Integer.MAX_VALUE);
    buffer.getAndSetInt(offset + SIZE_OF_INT, Integer.MAX_VALUE);
    buffer.putIntOrdered(offset + SIZE_OF_INT, Integer.MAX_VALUE);
    buffer.addIntOrdered(offset + SIZE_OF_INT, Integer.MAX_VALUE);

    buffer.getShortVolatile(offset + SIZE_OF_SHORT);
    buffer.putShortVolatile(offset + SIZE_OF_SHORT, Short.MAX_VALUE);
    buffer.getCharVolatile(offset + SIZE_OF_CHAR);
    buffer.putCharVolatile(offset + SIZE_OF_CHAR, Character.MAX_VALUE);
    buffer.getByteVolatile(offset + SIZE_OF_BYTE);
    buffer.putByteVolatile(offset + SIZE_OF_BYTE, Byte.MAX_VALUE);
}
 
Example 2
Source File: CountersReader.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * Iterate over all labels in the label buffer.
 *
 * @param consumer function to be called for each label.
 */
public void forEach(final IntObjConsumer<String> consumer)
{
    int counterId = 0;

    final AtomicBuffer metaDataBuffer = this.metaDataBuffer;
    for (int i = 0, capacity = metaDataBuffer.capacity(); i < capacity; i += METADATA_LENGTH)
    {
        final int recordStatus = metaDataBuffer.getIntVolatile(i);

        if (RECORD_ALLOCATED == recordStatus)
        {
            consumer.accept(counterId, labelValue(metaDataBuffer, i));
        }
        else if (RECORD_UNUSED == recordStatus)
        {
            break;
        }

        counterId++;
    }
}
 
Example 3
Source File: CountersReader.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * Iterate over the counters and provide the value and basic metadata.
 *
 * @param consumer for each allocated counter.
 */
public void forEach(final CounterConsumer consumer)
{
    int counterId = 0;

    final AtomicBuffer metaDataBuffer = this.metaDataBuffer;
    final AtomicBuffer valuesBuffer = this.valuesBuffer;

    for (int i = 0, capacity = metaDataBuffer.capacity(); i < capacity; i += METADATA_LENGTH)
    {
        final int recordStatus = metaDataBuffer.getIntVolatile(i);

        if (RECORD_ALLOCATED == recordStatus)
        {
            consumer.accept(
                valuesBuffer.getLongVolatile(counterOffset(counterId)), counterId, labelValue(metaDataBuffer, i));
        }
        else if (RECORD_UNUSED == recordStatus)
        {
            break;
        }

        counterId++;
    }
}
 
Example 4
Source File: CountersReader.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * Iterate over all the metadata in the buffer.
 *
 * @param metaData function to be called for each metadata record.
 */
public void forEach(final MetaData metaData)
{
    int counterId = 0;

    final AtomicBuffer metaDataBuffer = this.metaDataBuffer;

    for (int i = 0, capacity = metaDataBuffer.capacity(); i < capacity; i += METADATA_LENGTH)
    {
        final int recordStatus = metaDataBuffer.getIntVolatile(i);
        if (RECORD_ALLOCATED == recordStatus)
        {
            final int typeId = metaDataBuffer.getInt(i + TYPE_ID_OFFSET);
            final String label = labelValue(metaDataBuffer, i);
            final DirectBuffer keyBuffer = new UnsafeBuffer(metaDataBuffer, i + KEY_OFFSET, MAX_KEY_LENGTH);

            metaData.accept(counterId, typeId, keyBuffer, label);
        }
        else if (RECORD_UNUSED == recordStatus)
        {
            break;
        }

        counterId++;
    }
}
 
Example 5
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 6 votes vote down vote up
private static boolean scanBackToConfirmStillZeroed(final AtomicBuffer buffer, final int from, final int limit)
{
    int i = from - ALIGNMENT;
    boolean allZeros = true;
    while (i >= limit)
    {
        if (0 != buffer.getIntVolatile(i))
        {
            allZeros = false;
            break;
        }

        i -= ALIGNMENT;
    }

    return allZeros;
}
 
Example 6
Source File: CountersReader.java    From agrona with Apache License 2.0 5 votes vote down vote up
private String labelValue(final AtomicBuffer metaDataBuffer, final int recordOffset)
{
    final int labelLength = metaDataBuffer.getIntVolatile(recordOffset + LABEL_OFFSET);
    final byte[] stringInBytes = new byte[labelLength];
    metaDataBuffer.getBytes(recordOffset + LABEL_OFFSET + SIZE_OF_INT, stringInBytes);

    return new String(stringInBytes, labelCharset);
}
 
Example 7
Source File: ErrorLogReader.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Read all the errors in a log since a given timestamp.
 *
 * @param buffer         containing the {@link DistinctErrorLog}.
 * @param consumer       to be called for each exception encountered.
 * @param sinceTimestamp for filtering errors that have been recorded since this time.
 * @return the number of entries that has been read.
 */
public static int read(final AtomicBuffer buffer, final ErrorConsumer consumer, final long sinceTimestamp)
{
    int entries = 0;
    int offset = 0;
    final int capacity = buffer.capacity();

    while (offset < capacity)
    {
        final int length = buffer.getIntVolatile(offset + LENGTH_OFFSET);
        if (0 == length)
        {
            break;
        }

        final long lastObservationTimestamp = buffer.getLongVolatile(offset + LAST_OBSERVATION_TIMESTAMP_OFFSET);
        if (lastObservationTimestamp >= sinceTimestamp)
        {
            ++entries;

            consumer.accept(
                buffer.getInt(offset + OBSERVATION_COUNT_OFFSET),
                buffer.getLong(offset + FIRST_OBSERVATION_TIMESTAMP_OFFSET),
                lastObservationTimestamp,
                buffer.getStringWithoutLengthUtf8(offset + ENCODED_ERROR_OFFSET, length - ENCODED_ERROR_OFFSET));
        }

        offset += align(length, RECORD_ALIGNMENT);
    }

    return entries;
}
 
Example 8
Source File: OneToOneRingBuffer.java    From agrona with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public int read(final MessageHandler handler, final int messageCountLimit)
{
    int messagesRead = 0;

    final AtomicBuffer buffer = this.buffer;
    final int headPositionIndex = this.headPositionIndex;
    final long head = buffer.getLong(headPositionIndex);

    int bytesRead = 0;

    final int capacity = this.capacity;
    final int headIndex = (int)head & (capacity - 1);
    final int contiguousBlockLength = capacity - headIndex;

    try
    {
        while ((bytesRead < contiguousBlockLength) && (messagesRead < messageCountLimit))
        {
            final int recordIndex = headIndex + bytesRead;
            final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex));
            if (recordLength <= 0)
            {
                break;
            }

            bytesRead += align(recordLength, ALIGNMENT);

            final int messageTypeId = buffer.getInt(typeOffset(recordIndex));
            if (PADDING_MSG_TYPE_ID == messageTypeId)
            {
                continue;
            }

            ++messagesRead;
            handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH);
        }
    }
    finally
    {
        if (bytesRead != 0)
        {
            buffer.putLongOrdered(headPositionIndex, head + bytesRead);
        }
    }

    return messagesRead;
}
 
Example 9
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public int read(final MessageHandler handler, final int messageCountLimit)
{
    int messagesRead = 0;

    final AtomicBuffer buffer = this.buffer;
    final int headPositionIndex = this.headPositionIndex;
    final long head = buffer.getLong(headPositionIndex);

    final int capacity = this.capacity;
    final int headIndex = (int)head & (capacity - 1);
    final int maxBlockLength = capacity - headIndex;
    int bytesRead = 0;

    try
    {
        while ((bytesRead < maxBlockLength) && (messagesRead < messageCountLimit))
        {
            final int recordIndex = headIndex + bytesRead;
            final int recordLength = buffer.getIntVolatile(lengthOffset(recordIndex));
            if (recordLength <= 0)
            {
                break;
            }

            bytesRead += align(recordLength, ALIGNMENT);

            final int messageTypeId = buffer.getInt(typeOffset(recordIndex));
            if (PADDING_MSG_TYPE_ID == messageTypeId)
            {
                continue;
            }

            ++messagesRead;
            handler.onMessage(messageTypeId, buffer, recordIndex + HEADER_LENGTH, recordLength - HEADER_LENGTH);
        }
    }
    finally
    {
        if (bytesRead != 0)
        {
            buffer.setMemory(headIndex, bytesRead, (byte)0);
            buffer.putLongOrdered(headPositionIndex, head + bytesRead);
        }
    }

    return messagesRead;
}
 
Example 10
Source File: ManyToOneRingBuffer.java    From agrona with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
public boolean unblock()
{
    final AtomicBuffer buffer = this.buffer;
    final long headPosition = buffer.getLongVolatile(headPositionIndex);
    final long tailPosition = buffer.getLongVolatile(tailPositionIndex);

    if (headPosition == tailPosition)
    {
        return false;
    }

    final int mask = capacity - 1;
    final int consumerIndex = (int)(headPosition & mask);
    final int producerIndex = (int)(tailPosition & mask);

    boolean unblocked = false;
    int length = buffer.getIntVolatile(consumerIndex);
    if (length < 0)
    {
        buffer.putInt(typeOffset(consumerIndex), PADDING_MSG_TYPE_ID);
        buffer.putIntOrdered(lengthOffset(consumerIndex), -length);
        unblocked = true;
    }
    else if (0 == length)
    {
        // go from (consumerIndex to producerIndex) or (consumerIndex to capacity)
        final int limit = producerIndex > consumerIndex ? producerIndex : capacity;
        int i = consumerIndex + ALIGNMENT;

        do
        {
            // read the top int of every long (looking for length aligned to 8=ALIGNMENT)
            length = buffer.getIntVolatile(i);
            if (0 != length)
            {
                if (scanBackToConfirmStillZeroed(buffer, i, consumerIndex))
                {
                    buffer.putInt(typeOffset(consumerIndex), PADDING_MSG_TYPE_ID);
                    buffer.putIntOrdered(lengthOffset(consumerIndex), i - consumerIndex);
                    unblocked = true;
                }

                break;
            }

            i += ALIGNMENT;
        }
        while (i < limit);
    }

    return unblocked;
}
 
Example 11
Source File: ErrorLogReader.java    From agrona with Apache License 2.0 2 votes vote down vote up
/**
 * Has the error buffer any recorded errors?
 *
 * @param buffer containing the {@link DistinctErrorLog}.
 * @return true if there is at least one error.
 */
public static boolean hasErrors(final AtomicBuffer buffer)
{
    return buffer.capacity() > 0 && 0 != buffer.getIntVolatile(LENGTH_OFFSET);
}