<commit_before>/*
 * Copyright 2014 - 2015 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.real_logic.aeron.common;

import uk.co.real_logic.agrona.BitUtil;
import uk.co.real_logic.agrona.DirectBuffer;
import uk.co.real_logic.agrona.concurrent.UnsafeBuffer;

import java.nio.ByteBuffer;

/**
 * Description of the command and control file used between driver and clients
 *
 * File Layout
 * <pre>
 *  +----------------------------+
 *  |      Aeron CnC Version     |
 *  +----------------------------+
 *  |          Meta Data         |
 *  +----------------------------+
 *  |      to-driver Buffer      |
 *  +----------------------------+
 *  |      to-clients Buffer     |
 *  +----------------------------+
 *  |     Counter Labels Buffer  |
 *  +----------------------------+
 *  |     Counter Values Buffer  |
 *  +----------------------------+
 * </pre>
 *
 * Meta Data Layout (CnC Version 1)
 * <pre>
 *  +----------------------------+
 *  |   to-driver buffer length  |
 *  +----------------------------+
 *  |  to-clients buffer length  |
 *  +----------------------------+
 *  |    labels buffer length    |
 *  +----------------------------+
 *  |    values buffer length    |
 *  +----------------------------+
 * </pre>
 */
public class CncFileDescriptor
{
    public static final String CNC_FILE = "cnc";

    public static final int CNC_VERSION = 1;

    public static final int CNC_VERSION_FIELD_OFFSET;
    public static final int META_DATA_OFFSET;

    /* Meta Data Offsets (offsets within the meta data section) */

    public static final int TO_DRIVER_BUFFER_LENGTH_FIELD_OFFSET;
    public static final int TO_CLIENTS_BUFFER_LENGTH_FIELD_OFFSET;
    public static final int COUNTER_LABELS_BUFFER_LENGTH_FIELD_OFFSET;
    public static final int COUNTER_VALUES_BUFFER_LENGTH_FIELD_OFFSET;

    static
    {
        CNC_VERSION_FIELD_OFFSET = 0;
        META_DATA_OFFSET = CNC_VERSION_FIELD_OFFSET + BitUtil.SIZE_OF_INT;

        TO_DRIVER_BUFFER_LENGTH_FIELD_OFFSET = 0;
        TO_CLIENTS_BUFFER_LENGTH_FIELD_OFFSET = TO_DRIVER_BUFFER_LENGTH_FIELD_OFFSET + BitUtil.SIZE_OF_INT;
        COUNTER_LABELS_BUFFER_LENGTH_FIELD_OFFSET = TO_CLIENTS_BUFFER_LENGTH_FIELD_OFFSET + BitUtil.SIZE_OF_INT;
        COUNTER_VALUES_BUFFER_LENGTH_FIELD_OFFSET = COUNTER_LABELS_BUFFER_LENGTH_FIELD_OFFSET + BitUtil.SIZE_OF_INT;
    }

    public static final int META_DATA_LENGTH = COUNTER_VALUES_BUFFER_LENGTH_FIELD_OFFSET + BitUtil.SIZE_OF_INT;

    public static final int END_OF_META_DATA_OFFSET = BitUtil.SIZE_OF_INT + META_DATA_LENGTH;

    /**
     * Compute the length of the cnc file and return it.
     *
     * @param totalLengthOfBuffers in bytes
     * @return cnc file length in bytes
     */
    public static int computeCncFileLength(final int totalLengthOfBuffers)
    {
        return BitUtil.SIZE_OF_INT + META_DATA_LENGTH + totalLengthOfBuffers;
    }

    public static int cncVersionOffset(final int baseOffset)
    {
        return baseOffset + CNC_VERSION_FIELD_OFFSET;
    }

    public static int toDriverBufferLengthOffset(final int baseOffset)
    {
        return baseOffset + META_DATA_OFFSET + TO_DRIVER_BUFFER_LENGTH_FIELD_OFFSET;
    }

    public static int toClientsBufferLengthOffset(final int baseOffset)
    {
        return baseOffset + META_DATA_OFFSET + TO_CLIENTS_BUFFER_LENGTH_FIELD_OFFSET;
    }

    public static int counterLabelsBufferLengthOffset(final int baseOffset)
    {
        return baseOffset + META_DATA_OFFSET + COUNTER_LABELS_BUFFER_LENGTH_FIELD_OFFSET;
    }

    public static int counterValuesBufferLengthOffset(final int baseOffset)
    {
        return baseOffset + META_DATA_OFFSET + COUNTER_VALUES_BUFFER_LENGTH_FIELD_OFFSET;
    }

    public static UnsafeBuffer createMetaDataBuffer(final ByteBuffer buffer)
    {
        return new UnsafeBuffer(buffer, 0, BitUtil.SIZE_OF_INT + META_DATA_LENGTH);
    }

    public static void fillMetaData(
        final UnsafeBuffer cncMetaDataBuffer,
        final int toDriverBufferLength,
        final int toClientsBufferLength,
        final int counterLabelsBufferLength,
        final int counterValuesBufferLength)
    {
        cncMetaDataBuffer.putInt(cncVersionOffset(0), CncFileDescriptor.CNC_VERSION);
        cncMetaDataBuffer.putInt(toDriverBufferLengthOffset(0), toDriverBufferLength);
        cncMetaDataBuffer.putInt(toClientsBufferLengthOffset(0), toClientsBufferLength);
        cncMetaDataBuffer.putInt(counterLabelsBufferLengthOffset(0), counterLabelsBufferLength);
        cncMetaDataBuffer.putInt(counterValuesBufferLengthOffset(0), counterValuesBufferLength);
    }

    public static UnsafeBuffer createToDriverBuffer(final ByteBuffer buffer, final DirectBuffer metaDataBuffer)
    {
        return new UnsafeBuffer(buffer, END_OF_META_DATA_OFFSET, metaDataBuffer.getInt(toDriverBufferLengthOffset(0)));
    }

    public static UnsafeBuffer createToClientsBuffer(final ByteBuffer buffer, final DirectBuffer metaDataBuffer)
    {
        final int offset = END_OF_META_DATA_OFFSET +
            metaDataBuffer.getInt(toDriverBufferLengthOffset(0));

        return new UnsafeBuffer(buffer, offset, metaDataBuffer.getInt(toClientsBufferLengthOffset(0)));
    }<commit_msg>[Java]: Fix CnC buffer alignment for atomic access.<commit_after>/*
 * Copyright 2014 - 2015 Real Logic Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package uk.co.real_logic.aeron.common;

import uk.co.real_logic.agrona.BitUtil;
import uk.co.real_logic.agrona.DirectBuffer;
import uk.co.real_logic.agrona.concurrent.UnsafeBuffer;

import java.nio.ByteBuffer;

import static uk.co.real_logic.agrona.BitUtil.align;

/**
 * Description of the command and control file used between driver and clients
 *
 * File Layout
 * <pre>
 *  +----------------------------+
 *  |      Aeron CnC Version     |
 *  +----------------------------+
 *  |          Meta Data         |
 *  +----------------------------+
 *  |      to-driver Buffer      |
 *  +----------------------------+
 *  |      to-clients Buffer     |
 *  +----------------------------+
 *  |     Counter Labels Buffer  |
 *  +----------------------------+
 *  |     Counter Values Buffer  |
 *  +----------------------------+
 * </pre>
 *
 * Meta Data Layout (CnC Version 1)
 * <pre>
 *  +----------------------------+
 *  |   to-driver buffer length  |
 *  +----------------------------+
 *  |  to-clients buffer length  |
 *  +----------------------------+
 *  |    labels buffer length    |
 *  +----------------------------+
 *  |    values buffer length    |
 *  +----------------------------+
 * </pre>
 */
public class CncFileDescriptor
{
    public static final String CNC_FILE = "cnc";

    public static final int CNC_VERSION = 1;

    public static final int CNC_VERSION_FIELD_OFFSET;
    public static final int META_DATA_OFFSET;

    /* Meta Data Offsets (offsets within the meta data section) */

    public static final int TO_DRIVER_BUFFER_LENGTH_FIELD_OFFSET;
    public static final int TO_CLIENTS_BUFFER_LENGTH_FIELD_OFFSET;
    public static final int COUNTER_LABELS_BUFFER_LENGTH_FIELD_OFFSET;
    public static final int COUNTER_VALUES_BUFFER_LENGTH_FIELD_OFFSET;

    static
    {
        CNC_VERSION_FIELD_OFFSET = 0;
        META_DATA_OFFSET = CNC_VERSION_FIELD_OFFSET + BitUtil.SIZE_OF_INT;

        TO_DRIVER_BUFFER_LENGTH_FIELD_OFFSET = 0;
        TO_CLIENTS_BUFFER_LENGTH_FIELD_OFFSET = TO_DRIVER_BUFFER_LENGTH_FIELD_OFFSET + BitUtil.SIZE_OF_INT;
        COUNTER_LABELS_BUFFER_LENGTH_FIELD_OFFSET = TO_CLIENTS_BUFFER_LENGTH_FIELD_OFFSET + BitUtil.SIZE_OF_INT;
        COUNTER_VALUES_BUFFER_LENGTH_FIELD_OFFSET = COUNTER_LABELS_BUFFER_LENGTH_FIELD_OFFSET + BitUtil.SIZE_OF_INT;
    }

    public static final int META_DATA_LENGTH = COUNTER_VALUES_BUFFER_LENGTH_FIELD_OFFSET + BitUtil.SIZE_OF_INT;

    public static final int END_OF_META_DATA_OFFSET = align(BitUtil.SIZE_OF_INT + META_DATA_LENGTH, BitUtil.CACHE_LINE_LENGTH);

    /**
     * Compute the length of the cnc file and return it.
     *
     * @param totalLengthOfBuffers in bytes
     * @return cnc file length in bytes
     */
    public static int computeCncFileLength(final int totalLengthOfBuffers)
    {
        return END_OF_META_DATA_OFFSET + totalLengthOfBuffers;
    }

    public static int cncVersionOffset(final int baseOffset)
    {
        return baseOffset + CNC_VERSION_FIELD_OFFSET;
    }

    public static int toDriverBufferLengthOffset(final int baseOffset)
    {
        return baseOffset + META_DATA_OFFSET + TO_DRIVER_BUFFER_LENGTH_FIELD_OFFSET;
    }

    public static int toClientsBufferLengthOffset(final int baseOffset)
    {
        return baseOffset + META_DATA_OFFSET + TO_CLIENTS_BUFFER_LENGTH_FIELD_OFFSET;
    }

    public static int counterLabelsBufferLengthOffset(final int baseOffset)
    {
        return baseOffset + META_DATA_OFFSET + COUNTER_LABELS_BUFFER_LENGTH_FIELD_OFFSET;
    }

    public static int counterValuesBufferLengthOffset(final int baseOffset)
    {
        return baseOffset + META_DATA_OFFSET + COUNTER_VALUES_BUFFER_LENGTH_FIELD_OFFSET;
    }

    public static UnsafeBuffer createMetaDataBuffer(final ByteBuffer buffer)
    {
        return new UnsafeBuffer(buffer, 0, BitUtil.SIZE_OF_INT + META_DATA_LENGTH);
    }

    public static void fillMetaData(
        final UnsafeBuffer cncMetaDataBuffer,
        final int toDriverBufferLength,
        final int toClientsBufferLength,
        final int counterLabelsBufferLength,
        final int counterValuesBufferLength)
    {
        cncMetaDataBuffer.putInt(cncVersionOffset(0), CncFileDescriptor.CNC_VERSION);
        cncMetaDataBuffer.putInt(toDriverBufferLengthOffset(0), toDriverBufferLength);
        cncMetaDataBuffer.putInt(toClientsBufferLengthOffset(0), toClientsBufferLength);
        cncMetaDataBuffer.putInt(counterLabelsBufferLengthOffset(0), counterLabelsBufferLength);
        cncMetaDataBuffer.putInt(counterValuesBufferLengthOffset(0), counterValuesBufferLength);
    }

    public static UnsafeBuffer createToDriverBuffer(final ByteBuffer buffer, final DirectBuffer metaDataBuffer)
    {
        return new UnsafeBuffer(buffer, END_OF_META_DATA_OFFSET, metaDataBuffer.getInt(toDriverBufferLengthOffset(0)));
    }

    public static UnsafeBuffer createToClientsBuffer(final ByteBuffer buffer, final DirectBuffer metaDataBuffer)
    {
        final int offset = END_OF_META_DATA_OFFSET + metaDataBuffer.getInt(toDriverBufferLengthOffset(0));

        return new UnsafeBuffer(buffer, offset, metaDataBuffer.getInt(toClientsBufferLengthOffset(0)));
    }
{'fraction_non_alphanumeric': 0.12147355707014063, 'fraction_numerical': 0.004313691657320335, 'mean_word_length': 2.8270056124133376, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6205708', 'n_tokens_mistral': 3355, 'n_tokens_neox': 3250, 'n_words': 778}