Java Code Examples for org.agrona.BitUtil#isPowerOfTwo()

The following examples show how to use org.agrona.BitUtil#isPowerOfTwo() . 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: ReplayIndexDescriptor.java    From artio with Apache License 2.0 6 votes vote down vote up
static void checkIndexFileSize(final int indexFileSize)
{
    final int recordCapacity = recordCapacity(indexFileSize);
    if (!BitUtil.isPowerOfTwo(recordCapacity))
    {
        throw new IllegalStateException(
            "IndexFileSize must be a positive power of 2 + INITIAL_RECORD_OFFSET: indexFileSize=" + indexFileSize);
    }

    if ((recordCapacity % RECORD_LENGTH) != 0)
    {
        throw new IllegalStateException(
            "IndexFileSize must be a multiple of RECORD_LENGTH + INITIAL_RECORD_OFFSET: indexFileSize=" +
            indexFileSize);
    }
}
 
Example 2
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Check that term length is valid and alignment is valid.
 *
 * @param termLength to be checked.
 * @throws IllegalStateException if the length is not as expected.
 */
public static void checkTermLength(final int termLength)
{
    if (termLength < TERM_MIN_LENGTH)
    {
        throw new IllegalStateException(
            "Term length less than min length of " + TERM_MIN_LENGTH + ": length=" + termLength);
    }

    if (termLength > TERM_MAX_LENGTH)
    {
        throw new IllegalStateException(
            "Term length more than max length of " + TERM_MAX_LENGTH + ": length=" + termLength);
    }

    if (!BitUtil.isPowerOfTwo(termLength))
    {
        throw new IllegalStateException("Term length not a power of 2: length=" + termLength);
    }
}
 
Example 3
Source File: LogBufferDescriptor.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Check that page size is valid and alignment is valid.
 *
 * @param pageSize to be checked.
 * @throws IllegalStateException if the size is not as expected.
 */
public static void checkPageSize(final int pageSize)
{
    if (pageSize < PAGE_MIN_SIZE)
    {
        throw new IllegalStateException(
            "Page size less than min size of " + PAGE_MIN_SIZE + ": page size=" + pageSize);
    }

    if (pageSize > PAGE_MAX_SIZE)
    {
        throw new IllegalStateException(
            "Page size more than max size of " + PAGE_MAX_SIZE + ": page size=" + pageSize);
    }

    if (!BitUtil.isPowerOfTwo(pageSize))
    {
        throw new IllegalStateException("Page size not a power of 2: page size=" + pageSize);
    }
}
 
Example 4
Source File: Configuration.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Validate that page size is valid and alignment is valid.
 *
 * @param pageSize to be checked.
 * @throws ConfigurationException if the size is not as expected.
 */
public static void validatePageSize(final int pageSize)
{
    if (pageSize < PAGE_MIN_SIZE)
    {
        throw new ConfigurationException(
            "page size less than min size of " + PAGE_MIN_SIZE + ": " + pageSize);
    }

    if (pageSize > PAGE_MAX_SIZE)
    {
        throw new ConfigurationException(
            "page size greater than max size of " + PAGE_MAX_SIZE + ": " + pageSize);
    }

    if (!BitUtil.isPowerOfTwo(pageSize))
    {
        throw new ConfigurationException("page size not a power of 2: " + pageSize);
    }
}
 
Example 5
Source File: AeronUtil.java    From nd4j with Apache License 2.0 6 votes vote down vote up
/**
 * Get a media driver context
 * for sending ndarrays
 * based on a given length
 * where length is the length (number of elements)
 * in the ndarrays hat are being sent
 * @param length the length to based the ipc length
 * @return the media driver context based on the given length
 */
public static MediaDriver.Context getMediaDriverContext(int length) {
    //length of array * sizeof(float)
    int ipcLength = length * 16;
    //padding for NDArrayMessage
    ipcLength += 64;
    //must be a power of 2
    ipcLength *= 2;
    //ipc length must be positive power of 2
    while (!BitUtil.isPowerOfTwo(ipcLength))
        ipcLength += 2;
    // System.setProperty("aeron.term.buffer.size",String.valueOf(ipcLength));
    final MediaDriver.Context ctx =
                    new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirsDeleteOnStart(true)
                                    /*  .ipcTermBufferLength(ipcLength)
                                      .publicationTermBufferLength(ipcLength)
                                      .maxTermBufferLength(ipcLength)*/
                                    .conductorIdleStrategy(new BusySpinIdleStrategy())
                                    .receiverIdleStrategy(new BusySpinIdleStrategy())
                                    .senderIdleStrategy(new BusySpinIdleStrategy());
    return ctx;
}
 
Example 6
Source File: AeronUtil.java    From deeplearning4j with Apache License 2.0 6 votes vote down vote up
/**
 * Get a media driver context
 * for sending ndarrays
 * based on a given length
 * where length is the length (number of elements)
 * in the ndarrays hat are being sent
 * @param length the length to based the ipc length
 * @return the media driver context based on the given length
 */
public static MediaDriver.Context getMediaDriverContext(int length) {
    //length of array * sizeof(float)
    int ipcLength = length * 16;
    //padding for NDArrayMessage
    ipcLength += 64;
    //must be a power of 2
    ipcLength *= 2;
    //ipc length must be positive power of 2
    while (!BitUtil.isPowerOfTwo(ipcLength))
        ipcLength += 2;
    // System.setProperty("aeron.term.buffer.size",String.valueOf(ipcLength));
    final MediaDriver.Context ctx =
                    new MediaDriver.Context().threadingMode(ThreadingMode.SHARED).dirsDeleteOnStart(true)
                                    /*  .ipcTermBufferLength(ipcLength)
                                      .publicationTermBufferLength(ipcLength)
                                      .maxTermBufferLength(ipcLength)*/
                                    .conductorIdleStrategy(new BusySpinIdleStrategy())
                                    .receiverIdleStrategy(new BusySpinIdleStrategy())
                                    .senderIdleStrategy(new BusySpinIdleStrategy());
    return ctx;
}
 
Example 7
Source File: CollectionUtil.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Validate that a number is a power of two.
 *
 * @param value to be validated.
 */
public static void validatePositivePowerOfTwo(final int value)
{
    if (!BitUtil.isPowerOfTwo(value))
    {
        throw new IllegalArgumentException("value must be a positive power of two: " + value);
    }
}
 
Example 8
Source File: RingBufferDescriptor.java    From agrona with Apache License 2.0 5 votes vote down vote up
/**
 * Check the the buffer capacity is the correct size (a power of 2 + {@link RingBufferDescriptor#TRAILER_LENGTH}).
 *
 * @param capacity to be checked.
 * @throws IllegalStateException if the buffer capacity is incorrect.
 */
public static void checkCapacity(final int capacity)
{
    if (!BitUtil.isPowerOfTwo(capacity - TRAILER_LENGTH))
    {
        final String msg = "capacity must be a positive power of 2 + TRAILER_LENGTH: capacity=" + capacity;
        throw new IllegalStateException(msg);
    }
}