org.agrona.BufferUtil Java Examples

The following examples show how to use org.agrona.BufferUtil. 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: DataHeaderFlyweight.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Return an initialised default Data Frame Header.
 *
 * @param sessionId for the header
 * @param streamId  for the header
 * @param termId    for the header
 * @return byte array containing the header
 */
public static UnsafeBuffer createDefaultHeader(final int sessionId, final int streamId, final int termId)
{
    final UnsafeBuffer buffer = new UnsafeBuffer(
        BufferUtil.allocateDirectAligned(HEADER_LENGTH, CACHE_LINE_LENGTH));

    buffer.putByte(VERSION_FIELD_OFFSET, CURRENT_VERSION);
    buffer.putByte(FLAGS_FIELD_OFFSET, (byte)BEGIN_AND_END_FLAGS);
    buffer.putShort(TYPE_FIELD_OFFSET, (short)HDR_TYPE_DATA, LITTLE_ENDIAN);
    buffer.putInt(SESSION_ID_FIELD_OFFSET, sessionId, LITTLE_ENDIAN);
    buffer.putInt(STREAM_ID_FIELD_OFFSET, streamId, LITTLE_ENDIAN);
    buffer.putInt(TERM_ID_FIELD_OFFSET, termId, LITTLE_ENDIAN);
    buffer.putLong(RESERVED_VALUE_OFFSET, DEFAULT_RESERVE_VALUE);

    return buffer;
}
 
Example #2
Source File: MappedResizeableBuffer.java    From agrona with Apache License 2.0 6 votes vote down vote up
public void getBytes(final long index, final ByteBuffer dstBuffer, final int length)
{
    final int dstOffset = dstBuffer.position();
    if (SHOULD_BOUNDS_CHECK)
    {
        boundsCheck0(index, length);
        BufferUtil.boundsCheck(dstBuffer, dstOffset, length);
    }

    final byte[] dstByteArray;
    final long dstBaseOffset;
    if (dstBuffer.isDirect())
    {
        dstByteArray = null;
        dstBaseOffset = address(dstBuffer);
    }
    else
    {
        dstByteArray = array(dstBuffer);
        dstBaseOffset = ARRAY_BASE_OFFSET + arrayOffset(dstBuffer);
    }

    UNSAFE.copyMemory(null, addressOffset + index, dstByteArray, dstBaseOffset + dstOffset, length);
    dstBuffer.position(dstBuffer.position() + length);
}
 
Example #3
Source File: MappedResizeableBuffer.java    From agrona with Apache License 2.0 6 votes vote down vote up
public void putBytes(final long index, final ByteBuffer srcBuffer, final long srcIndex, final int length)
{
    if (SHOULD_BOUNDS_CHECK)
    {
        boundsCheck0(index, length);
        BufferUtil.boundsCheck(srcBuffer, srcIndex, length);
    }

    final byte[] srcByteArray;
    final long srcBaseOffset;
    if (srcBuffer.isDirect())
    {
        srcByteArray = null;
        srcBaseOffset = address(srcBuffer);
    }
    else
    {
        srcByteArray = array(srcBuffer);
        srcBaseOffset = ARRAY_BASE_OFFSET + arrayOffset(srcBuffer);
    }

    UNSAFE.copyMemory(srcByteArray, srcBaseOffset + srcIndex, null, addressOffset + index, length);
}
 
Example #4
Source File: ArraysFillBenchmark.java    From artio with Apache License 2.0 5 votes vote down vote up
@Benchmark
public int[] memset()
{
    final int[] values = this.values;
    UNSAFE.setMemory(values, BufferUtil.ARRAY_BASE_OFFSET, sizeInBytes, MISSING_BYTE);
    return values;
}
 
Example #5
Source File: ArraysFillBenchmark.java    From artio with Apache License 2.0 5 votes vote down vote up
@Benchmark
public int[] offsetMemset()
{
    final int[] values = this.values;
    UNSAFE.putByte(values, BufferUtil.ARRAY_BASE_OFFSET, MISSING_BYTE);
    UNSAFE.setMemory(values, BufferUtil.ARRAY_BASE_OFFSET + 1, sizeInBytes - 1, MISSING_BYTE);
    return values;
}
 
Example #6
Source File: NetworkPublicationThreadLocals.java    From aeron with Apache License 2.0 5 votes vote down vote up
NetworkPublicationThreadLocals()
{
    final ByteBuffer byteBuffer = BufferUtil.allocateDirectAligned(CACHE_LINE_LENGTH * 3, CACHE_LINE_LENGTH);

    byteBuffer.limit(DataHeaderFlyweight.HEADER_LENGTH);
    heartbeatBuffer = byteBuffer.slice();
    dataHeader = new DataHeaderFlyweight(heartbeatBuffer);

    int position = CACHE_LINE_LENGTH;
    byteBuffer.limit(position + SetupFlyweight.HEADER_LENGTH).position(position);
    setupBuffer = byteBuffer.slice();
    setupHeader = new SetupFlyweight(setupBuffer);

    position += CACHE_LINE_LENGTH;
    byteBuffer.limit(position + RttMeasurementFlyweight.HEADER_LENGTH).position(position);
    rttMeasurementBuffer = byteBuffer.slice();
    rttMeasurementHeader = new RttMeasurementFlyweight(rttMeasurementBuffer);

    dataHeader
        .version(HeaderFlyweight.CURRENT_VERSION)
        .flags((byte)DataHeaderFlyweight.BEGIN_AND_END_FLAGS)
        .headerType(HeaderFlyweight.HDR_TYPE_DATA)
        .frameLength(0);

    setupHeader
        .version(HeaderFlyweight.CURRENT_VERSION)
        .headerType(HeaderFlyweight.HDR_TYPE_SETUP)
        .frameLength(SetupFlyweight.HEADER_LENGTH);

    rttMeasurementHeader
        .version(HeaderFlyweight.CURRENT_VERSION)
        .headerType(HeaderFlyweight.HDR_TYPE_RTTM)
        .frameLength(RttMeasurementFlyweight.HEADER_LENGTH);
}
 
Example #7
Source File: NetworkUtil.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Allocate a direct {@link ByteBuffer} that is padded at the end with at least alignment bytes.
 *
 * @param capacity  for the buffer.
 * @param alignment for the buffer.
 * @return the direct {@link ByteBuffer}.
 */
public static ByteBuffer allocateDirectAlignedAndPadded(final int capacity, final int alignment)
{
    final ByteBuffer buffer = BufferUtil.allocateDirectAligned(capacity + alignment, alignment);

    buffer.limit(buffer.limit() - alignment);

    return buffer.slice();
}
 
Example #8
Source File: MappedResizeableBuffer.java    From agrona with Apache License 2.0 5 votes vote down vote up
public void getBytes(final long index, final byte[] dst, final long offset, final int length)
{
    if (SHOULD_BOUNDS_CHECK)
    {
        boundsCheck0(index, length);
        BufferUtil.boundsCheck(dst, offset, length);
    }

    UNSAFE.copyMemory(null, addressOffset + index, dst, ARRAY_BASE_OFFSET + offset, length);
}
 
Example #9
Source File: MappedResizeableBuffer.java    From agrona with Apache License 2.0 5 votes vote down vote up
public void putBytes(final long index, final byte[] src, final long offset, final int length)
{
    if (SHOULD_BOUNDS_CHECK)
    {
        boundsCheck0(index, length);
        BufferUtil.boundsCheck(src, offset, length);
    }

    UNSAFE.copyMemory(src, ARRAY_BASE_OFFSET + offset, null, addressOffset + index, length);
}
 
Example #10
Source File: MappedResizeableBuffer.java    From agrona with Apache License 2.0 5 votes vote down vote up
public void putBytes(final long index, final ByteBuffer srcBuffer, final int length)
{
    final int srcIndex = srcBuffer.position();
    if (SHOULD_BOUNDS_CHECK)
    {
        boundsCheck0(index, length);
        BufferUtil.boundsCheck(srcBuffer, srcIndex, length);
    }

    putBytes(index, srcBuffer, srcIndex, length);
    srcBuffer.position(srcIndex + length);
}
 
Example #11
Source File: JnaDirectNativeClock.java    From maestro-java with Apache License 2.0 4 votes vote down vote up
public JnaDirectNativeClock() {
    buffer = new UnsafeBuffer(BufferUtil.allocateDirectAligned(TIMESPEC_REQUIRED_CAPACITY, Long.BYTES));
    this.pointer = Pointer.createConstant(buffer.addressOffset());
}
 
Example #12
Source File: SimplePublisher.java    From aeron with Apache License 2.0 4 votes vote down vote up
public static void main(final String[] args) throws Exception
{
    // Allocate enough buffer size to hold maximum message length
    // The UnsafeBuffer class is part of the Agrona library and is used for efficient buffer management
    final UnsafeBuffer buffer = new UnsafeBuffer(BufferUtil.allocateDirectAligned(512, BitUtil.CACHE_LINE_LENGTH));

    // The channel (an endpoint identifier) to send the message to
    final String channel = "aeron:udp?endpoint=localhost:40123";

    // A unique identifier for a stream within a channel. Stream ID 0 is reserved
    // for internal use and should not be used by applications.
    final int streamId = 10;

    System.out.println("Publishing to " + channel + " on stream id " + streamId);

    // Create a context, needed for client connection to media driver
    // A separate media driver process needs to be running prior to starting this application
    final Aeron.Context ctx = new Aeron.Context();

    // Create an Aeron instance with client-provided context configuration and connect to the
    // media driver, and create a Publication.  The Aeron and Publication classes implement
    // AutoCloseable, and will automatically clean up resources when this try block is finished.
    try (Aeron aeron = Aeron.connect(ctx);
        Publication publication = aeron.addPublication(channel, streamId))
    {
        final String message = "Hello World! ";
        final byte[] messageBytes = message.getBytes();
        buffer.putBytes(0, messageBytes);

        // Wait for 5 seconds to connect to a subscriber
        final long deadlineNs = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
        while (!publication.isConnected())
        {
            if ((deadlineNs - System.nanoTime()) < 0)
            {
                System.out.println("Failed to connect to subscriber");
                return;
            }

            Thread.sleep(1);
        }

        // Try to publish the buffer. 'offer' is a non-blocking call.
        // If it returns less than 0, the message was not sent, and the offer should be retried.
        final long result = publication.offer(buffer, 0, messageBytes.length);

        if (result < 0L)
        {
            if (result == Publication.BACK_PRESSURED)
            {
                System.out.println(" Offer failed due to back pressure");
            }
            else if (result == Publication.NOT_CONNECTED)
            {
                System.out.println(" Offer failed because publisher is not connected to subscriber");
            }
            else if (result == Publication.ADMIN_ACTION)
            {
                System.out.println("Offer failed because of an administration action in the system");
            }
            else if (result == Publication.CLOSED)
            {
                System.out.println("Offer failed publication is closed");
            }
            else if (result == Publication.MAX_POSITION_EXCEEDED)
            {
                System.out.println("Offer failed due to publication reaching max position");
            }
            else
            {
                System.out.println(" Offer failed due to unknown reason");
            }
        }
        else
        {
            System.out.println(" yay !!");
        }

        System.out.println("Done sending.");
    }
}
 
Example #13
Source File: EmbeddedExclusiveVectoredIpcThroughput.java    From aeron with Apache License 2.0 4 votes vote down vote up
public void run()
{
    final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy();
    final AtomicBoolean running = this.running;
    final ExclusivePublication publication = this.publication;
    final ByteBuffer byteBuffer = BufferUtil.allocateDirectAligned(MESSAGE_LENGTH, CACHE_LINE_LENGTH);
    final UnsafeBuffer bufferOne = new UnsafeBuffer(byteBuffer, 0, VEC_ONE_LENGTH);
    final UnsafeBuffer bufferTwo = new UnsafeBuffer(byteBuffer, VEC_ONE_LENGTH, VEC_TWO_LENGTH);
    final DirectBufferVector[] vectors = new DirectBufferVector[]
    {
        new DirectBufferVector(bufferOne, 0, VEC_ONE_LENGTH),
        new DirectBufferVector(bufferTwo, 0, VEC_TWO_LENGTH),
    };

    long backPressureCount = 0;
    long totalMessageCount = 0;

    outputResults:
    while (running.get())
    {
        for (int i = 0; i < BURST_LENGTH; i++)
        {
            idleStrategy.reset();
            while (publication.offer(vectors, null) <= 0)
            {
                ++backPressureCount;
                if (!running.get())
                {
                    break outputResults;
                }

                idleStrategy.idle();
            }

            ++totalMessageCount;
        }
    }

    final double backPressureRatio = backPressureCount / (double)totalMessageCount;
    System.out.format("Publisher back pressure ratio: %f%n", backPressureRatio);
}
 
Example #14
Source File: ReceiveChannelEndpointThreadLocals.java    From aeron with Apache License 2.0 4 votes vote down vote up
public ReceiveChannelEndpointThreadLocals()
{
    final int smLength = StatusMessageFlyweight.HEADER_LENGTH + SIZE_OF_LONG;
    final int bufferLength =
        BitUtil.align(smLength, CACHE_LINE_LENGTH) +
        BitUtil.align(NakFlyweight.HEADER_LENGTH, CACHE_LINE_LENGTH) +
        BitUtil.align(RttMeasurementFlyweight.HEADER_LENGTH, CACHE_LINE_LENGTH);

    final UUID uuid = UUID.randomUUID();
    nextReceiverId = uuid.getMostSignificantBits() ^ uuid.getLeastSignificantBits();

    final ByteBuffer byteBuffer = BufferUtil.allocateDirectAligned(bufferLength, CACHE_LINE_LENGTH);

    byteBuffer.limit(smLength);
    smBuffer = byteBuffer.slice();
    statusMessageFlyweight = new StatusMessageFlyweight(smBuffer);

    final int nakMessageOffset = BitUtil.align(smLength, FRAME_ALIGNMENT);
    byteBuffer.limit(nakMessageOffset + NakFlyweight.HEADER_LENGTH).position(nakMessageOffset);
    nakBuffer = byteBuffer.slice();
    nakFlyweight = new NakFlyweight(nakBuffer);

    final int rttMeasurementOffset = nakMessageOffset + BitUtil.align(NakFlyweight.HEADER_LENGTH, FRAME_ALIGNMENT);
    byteBuffer.limit(rttMeasurementOffset + RttMeasurementFlyweight.HEADER_LENGTH).position(rttMeasurementOffset);
    rttMeasurementBuffer = byteBuffer.slice();
    rttMeasurementFlyweight = new RttMeasurementFlyweight(rttMeasurementBuffer);

    statusMessageFlyweight
        .version(HeaderFlyweight.CURRENT_VERSION)
        .headerType(HeaderFlyweight.HDR_TYPE_SM)
        .frameLength(StatusMessageFlyweight.HEADER_LENGTH);

    nakFlyweight
        .version(HeaderFlyweight.CURRENT_VERSION)
        .headerType(HeaderFlyweight.HDR_TYPE_NAK)
        .frameLength(NakFlyweight.HEADER_LENGTH);

    rttMeasurementFlyweight
        .version(HeaderFlyweight.CURRENT_VERSION)
        .headerType(HeaderFlyweight.HDR_TYPE_RTTM)
        .frameLength(RttMeasurementFlyweight.HEADER_LENGTH);
}