final MutableInteger id = new MutableInteger(-1); countersReader.forEach( (counterId, typeId, keyBuffer, label) -> statusReader = new UnsafeBufferStatusIndicator(countersReader.valuesBuffer(), id.value);
long channelStatus(final int channelStatusId) { switch (channelStatusId) { case 0: return ChannelEndpointStatus.INITIALIZING; case ChannelEndpointStatus.NO_ID_ALLOCATED: return ChannelEndpointStatus.ACTIVE; default: return countersReader.getCounterValue(channelStatusId); } }
/** * Construct a view of an existing counter. * * @param countersReader for getting access to the buffers. * @param registrationId assigned by the driver for the counter or {@link Aeron#NULL_VALUE} if not known. * @param counterId for the counter to be viewed. * @throws IllegalStateException if the id has for the counter has not been allocated. */ public ReadableCounter(final CountersReader countersReader, final long registrationId, final int counterId) { if (countersReader.getCounterState(counterId) != CountersReader.RECORD_ALLOCATED) { throw new IllegalStateException("Counter id has not been allocated: " + counterId); } this.countersReader = countersReader; this.counterId = counterId; this.registrationId = registrationId; final AtomicBuffer valuesBuffer = countersReader.valuesBuffer(); final int counterOffset = CountersReader.counterOffset(counterId); valuesBuffer.boundsCheck(counterOffset, SIZE_OF_LONG); this.buffer = valuesBuffer.byteArray(); this.addressOffset = valuesBuffer.addressOffset() + counterOffset; }
/** * Get the label for a given counter id. * * @param counterId to be read. * @return the label for the given counter id. */ public String getCounterLabel(final int counterId) { validateCounterId(counterId); return labelValue(metaDataOffset(counterId)); }
/** * Find the active counter id for recovery state. * * @param counters to search within. * @return the counter id if found otherwise {@link CountersReader#NULL_COUNTER_ID}. */ public static int findCounterId(final CountersReader counters) { final DirectBuffer buffer = counters.metaDataBuffer(); for (int i = 0, size = counters.maxCounterId(); i < size; i++) { if (counters.getCounterState(i) == RECORD_ALLOCATED) { final int recordOffset = CountersReader.metaDataOffset(i); if (buffer.getInt(recordOffset + TYPE_ID_OFFSET) == RECOVERY_STATE_TYPE_ID) { return i; } } } return NULL_COUNTER_ID; }
public void print(final PrintStream out) { counters.forEach( (counterId, typeId, keyBuffer, label) -> { if (filter(typeId, keyBuffer)) { final long value = counters.getCounterValue(counterId); out.format("%3d: %,20d - %s%n", counterId, value, label); } }); }
/** * Get the {@link Image#sourceIdentity()} for the recording. * * @param countersReader to search within. * @param counterId for the active recording. * @return {@link Image#sourceIdentity()} for the recording or null if not found. */ public static String getSourceIdentity(final CountersReader countersReader, final int counterId) { final DirectBuffer buffer = countersReader.metaDataBuffer(); if (countersReader.getCounterState(counterId) == RECORD_ALLOCATED) { final int recordOffset = CountersReader.metaDataOffset(counterId); if (buffer.getInt(recordOffset + TYPE_ID_OFFSET) == RECORDING_POSITION_TYPE_ID) { return buffer.getStringAscii(recordOffset + KEY_OFFSET + SOURCE_IDENTITY_LENGTH_OFFSET); } } return null; }
/** * Print out the values from {@link #countersReader()} which can be useful for debugging. * * @param out to where the counters get printed. */ public void printCounters(final PrintStream out) { final CountersReader counters = countersReader(); counters.forEach((value, id, label) -> out.format("%3d: %,20d - %s%n", id, value, label)); }
/** * Construct a read-write view of an existing counter. * * @param countersReader for getting access to the buffers. * @param registrationId assigned by the driver for the counter or {@link Aeron#NULL_VALUE} if not known. * @param counterId for the counter to be viewed. * @throws AeronException if the id has for the counter has not been allocated. */ Counter( final CountersReader countersReader, final long registrationId, final int counterId) { super(countersReader.valuesBuffer(), counterId); if (countersReader.getCounterState(counterId) != CountersReader.RECORD_ALLOCATED) { throw new AeronException("Counter id has not been allocated: " + counterId); } this.registrationId = registrationId; this.clientConductor = null; }
public static CountersReader mapCounters() { final File cncFile = CommonContext.newDefaultCncFile(); System.out.println("Command `n Control file " + cncFile); final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "cnc"); final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer); final int cncVersion = cncMetaData.getInt(cncVersionOffset(0)); if (CncFileDescriptor.CNC_VERSION != cncVersion) { throw new IllegalStateException("CnC version not supported: file version=" + cncVersion); } return new CountersReader( createCountersMetaDataBuffer(cncByteBuffer, cncMetaData), createCountersValuesBuffer(cncByteBuffer, cncMetaData)); }
/** * Get the deadline (in milliseconds) for when a given counter id may be reused. * * @param counterId to be read. * @return deadline (in milliseconds) for when a given counter id may be reused or {@link #NOT_FREE_TO_REUSE} if * currently in use. */ public long getFreeForReuseDeadline(final int counterId) { validateCounterId(counterId); return metaDataBuffer.getLongVolatile(metaDataOffset(counterId) + FREE_FOR_REUSE_DEADLINE_OFFSET); }
/** * Get the value for a given counter id as a volatile read. * * @param counterId to be read. * @return the current value of the counter. */ public long getCounterValue(final int counterId) { validateCounterId(counterId); return valuesBuffer.getLongVolatile(counterOffset(counterId)); }
/** * 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; 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(i)); } else if (RECORD_UNUSED == recordStatus) { break; } counterId++; } }
/** * Return the state of the counter. * * @see CountersReader#RECORD_ALLOCATED * @see CountersReader#RECORD_RECLAIMED * @see CountersReader#RECORD_UNUSED * * @return state for the counter. */ public int state() { return countersReader.getCounterState(counterId); }
private AtomicCounter awaitHeartbeatCounter(final CountersReader counters) { idleStrategy.reset(); int counterId = ServiceHeartbeat.findCounterId(counters, ctx.serviceId()); while (NULL_COUNTER_ID == counterId) { checkInterruptedStatus(); idleStrategy.idle(); counterId = ServiceHeartbeat.findCounterId(counters, ctx.serviceId()); } return new AtomicCounter(counters.valuesBuffer(), counterId); }
/** * Get the value for a given counter id as a volatile read. * * @param counterId to be read. * @return the current value of the counter. */ public long getCounterValue(final int counterId) { return valuesBuffer.getLongVolatile(counterOffset(counterId)); }
/** * Return the counter label. * * @return the counter label. */ public String label() { return countersReader.getCounterLabel(counterId); }
/** * Find the control toggle counter or return null if not found. * * @param counters to search for the control toggle. * @return the control toggle counter or return null if not found. */ public static AtomicCounter findControlToggle(final CountersReader counters) { final AtomicBuffer buffer = counters.metaDataBuffer(); for (int i = 0, size = counters.maxCounterId(); i < size; i++) { final int recordOffset = CountersReader.metaDataOffset(i); if (counters.getCounterState(i) == RECORD_ALLOCATED && buffer.getInt(recordOffset + TYPE_ID_OFFSET) == CONTROL_TOGGLE_TYPE_ID) { return new AtomicCounter(counters.valuesBuffer(), i, null); } } return null; }
/** * Get the position at which the snapshot was taken. {@link Aeron#NULL_VALUE} if no snapshot for recovery. * * @param counters to search within. * @param counterId for the active recovery counter. * @return the log position if found otherwise {@link Aeron#NULL_VALUE}. */ public static long getLogPosition(final CountersReader counters, final int counterId) { final DirectBuffer buffer = counters.metaDataBuffer(); if (counters.getCounterState(counterId) == RECORD_ALLOCATED) { final int recordOffset = CountersReader.metaDataOffset(counterId); if (buffer.getInt(recordOffset + TYPE_ID_OFFSET) == RECOVERY_STATE_TYPE_ID) { return buffer.getLong(recordOffset + KEY_OFFSET + LOG_POSITION_OFFSET); } } return NULL_VALUE; }
/** * Take a snapshot of all the counters and group them by streams. * * @return a snapshot of all the counters and group them by streams. */ public Map<StreamCompositeKey, List<StreamPosition>> snapshot() { final Map<StreamCompositeKey, List<StreamPosition>> streams = new HashMap<>(); counters.forEach( (counterId, typeId, keyBuffer, label) -> { if ((typeId >= PUBLISHER_LIMIT_TYPE_ID && typeId <= RECEIVER_POS_TYPE_ID) || typeId == SENDER_LIMIT_TYPE_ID || typeId == PER_IMAGE_TYPE_ID || typeId == PUBLISHER_POS_TYPE_ID) { final StreamCompositeKey key = new StreamCompositeKey( keyBuffer.getInt(SESSION_ID_OFFSET), keyBuffer.getInt(STREAM_ID_OFFSET), keyBuffer.getStringAscii(CHANNEL_OFFSET)); final StreamPosition position = new StreamPosition( keyBuffer.getLong(REGISTRATION_ID_OFFSET), counters.getCounterValue(counterId), typeId); streams .computeIfAbsent(key, (ignore) -> new ArrayList<>()) .add(position); } }); return streams; }