use of io.engineblock.activityapi.cyclelog.buffers.results.CycleResultsSegment in project engineblock by engineblock.
the class ByteTrackerExtentTest method testOrdered4.
@Test
public void testOrdered4() {
ByteTrackerExtent bt4 = new ByteTrackerExtent(33, 37);
bt4.markResult(36L, 3);
bt4.markResult(33L, 0);
bt4.markResult(35L, 2);
assertThat(bt4.getMarkerData()[3]).isEqualTo((byte) 3);
assertThat(bt4.getMarkerData()[0]).isEqualTo((byte) 0);
assertThat(bt4.getMarkerData()[2]).isEqualTo((byte) 2);
assertThat(bt4.isFullyFilled()).isFalse();
bt4.markResult(34L, 1);
assertThat(bt4.getMarkerData()[1]).isEqualTo((byte) 1);
assertThat(bt4.isFullyFilled()).isTrue();
List<CycleResult> cycleResults = new ArrayList<>();
List<CycleResultsSegment> segments = StreamSupport.stream(bt4.spliterator(), false).collect(Collectors.toList());
for (CycleResultsSegment segment : segments) {
segment.forEach(cycleResults::add);
}
long[] cycles = cycleResults.stream().mapToLong(CycleResult::getCycle).toArray();
int[] results = cycleResults.stream().mapToInt(CycleResult::getResult).toArray();
assertThat(cycles).containsExactly(33L, 34L, 35L, 36L);
assertThat(results).containsExactly(0, 1, 2, 3);
// CycleResultsSegment seg1 = iterator;
// assertThat(seg1.cycle).isEqualTo(33);
// assertThat(seg1.codes.length).isEqualTo(2);
// assertThat(seg1.codes[0]).isEqualTo((byte)0);
// assertThat(bt4.isFullyServed()).isFalse();
// CycleResultsIntervalSegment seg2 = bt4.getCycleResultsSegment(2);
// assertThat(bt4.isFullyServed()).isTrue();
}
use of io.engineblock.activityapi.cyclelog.buffers.results.CycleResultsSegment in project engineblock by engineblock.
the class CycleResultsRLEBufferReadableTest method testGetCycleResultsSegment.
@Test
public void testGetCycleResultsSegment() {
CycleResultsRLEBufferTarget t = new CycleResultsRLEBufferTarget(1000);
t.onCycleResult(1L, 1);
t.onCycleResult(2L, 1);
t.onCycleResult(10L, 2);
t.onCycleResult(11L, 2);
t.onCycleResult(13L, 2);
t.onCycleResult(7L, 7);
CycleResultsRLEBufferReadable readable = t.toSegmentsReadable();
CycleResultsSegment s1 = readable.iterator().next();
}
use of io.engineblock.activityapi.cyclelog.buffers.results.CycleResultsSegment in project engineblock by engineblock.
the class CoreMotor method run.
@Override
public void run() {
try {
Timer cyclesTimer = ActivityMetrics.timer(activity.getActivityDef(), "cycles");
Timer phasesTimer = ActivityMetrics.timer(activity.getActivityDef(), "phases");
Timer stridesTimer = ActivityMetrics.timer(activity.getActivityDef(), "strides");
Timer inputTimer = ActivityMetrics.timer(activity.getActivityDef(), "read-input");
strideRateLimiter = activity.getStrideLimiter();
cycleRateLimiter = activity.getCycleLimiter();
phaseRateLimiter = activity.getPhaseLimiter();
if (slotState.get() == Finished) {
logger.warn("Input was already exhausted for slot " + slotId + ", remaining in finished state.");
}
slotStateTracker.enterState(Running);
MultiPhaseAction multiPhaseAction = null;
if (action instanceof MultiPhaseAction) {
multiPhaseAction = ((MultiPhaseAction) action);
}
long cyclenum;
action.init();
if (input instanceof Startable) {
((Startable) input).start();
}
if (strideRateLimiter != null) {
// block for strides rate limiter
strideRateLimiter.start();
}
long strideDelay = 0L;
long cycleDelay = 0L;
long phaseDelay = 0L;
while (slotState.get() == Running) {
CycleSegment cycleSegment = null;
try (Timer.Context inputTime = inputTimer.time()) {
cycleSegment = input.getInputSegment(stride);
}
if (cycleSegment == null) {
logger.debug("input exhausted (input " + input + ") via null segment, stopping motor thread " + slotId);
slotStateTracker.enterState(Finished);
continue;
}
CycleResultSegmentBuffer segBuffer = new CycleResultSegmentBuffer(stride);
if (strideRateLimiter != null) {
// block for strides rate limiter
strideDelay = strideRateLimiter.acquire();
}
// try (Timer.Context stridesTime = stridesTimer.time()) {
long strideStart = System.nanoTime();
try {
while (!cycleSegment.isExhausted()) {
cyclenum = cycleSegment.nextCycle();
if (cyclenum < 0) {
if (cycleSegment.isExhausted()) {
logger.trace("input exhausted (input " + input + ") via negative read, stopping motor thread " + slotId);
slotStateTracker.enterState(Finished);
continue;
}
}
if (slotState.get() != Running) {
logger.trace("motor stopped after input (input " + cyclenum + "), stopping motor thread " + slotId);
continue;
}
int result = -1;
if (cycleRateLimiter != null) {
// Block for cycle rate limiter
cycleDelay = cycleRateLimiter.acquire();
}
// Phases are rate limited independently from overall cycles, but each cycle has at least one phase.
if (phaseRateLimiter != null) {
// Block for cycle rate limiter
phaseDelay = phaseRateLimiter.acquire();
}
// try (Timer.Context cycleTime = cyclesTimer.time()) {
long cycleStart = System.nanoTime();
try {
logger.trace("cycle " + cyclenum);
try (Timer.Context phaseTime = phasesTimer.time()) {
result = action.runCycle(cyclenum);
}
if (multiPhaseAction != null) {
while (multiPhaseAction.incomplete()) {
if (phaseRateLimiter != null) {
// Block for cycle rate limiter
phaseDelay = phaseRateLimiter.acquire();
}
try (Timer.Context phaseTime = phasesTimer.time()) {
result = multiPhaseAction.runPhase(cyclenum);
}
}
}
} finally {
long cycleEnd = System.nanoTime();
cyclesTimer.update((cycleEnd - cycleStart) + cycleDelay, TimeUnit.NANOSECONDS);
}
segBuffer.append(cyclenum, result);
}
} finally {
long strideEnd = System.nanoTime();
stridesTimer.update((strideEnd - strideStart) + strideDelay, TimeUnit.NANOSECONDS);
}
if (output != null) {
CycleResultsSegment outputBuffer = segBuffer.toReader();
try {
output.onCycleResultSegment(outputBuffer);
} catch (Exception t) {
logger.error("Error while feeding result segment " + outputBuffer + " to output '" + output + "', error:" + t);
throw t;
}
}
}
if (slotState.get() == Stopping) {
slotStateTracker.enterState(Stopped);
}
} catch (Throwable t) {
logger.error("Error in core motor loop:" + t, t);
throw t;
}
}
use of io.engineblock.activityapi.cyclelog.buffers.results.CycleResultsSegment in project engineblock by engineblock.
the class CycleResultsArraySegmentReadableTest method testCycleResultSegmentReader.
@Test
public void testCycleResultSegmentReader() {
CycleResultSegmentBuffer bb = new CycleResultSegmentBuffer(5);
bb.append(33L, 0);
bb.append(34L, 1);
bb.append(35L, 2);
bb.append(36L, 1);
bb.append(39L, 0);
CycleResultsSegment cycleResults = bb.toReader();
long[] cycles = StreamSupport.stream(cycleResults.spliterator(), false).mapToLong(CycleResult::getCycle).toArray();
int[] results = StreamSupport.stream(cycleResults.spliterator(), false).mapToInt(CycleResult::getResult).toArray();
assertThat(cycles).containsExactly(33L, 34L, 35L, 36L, 39L);
assertThat(results).containsExactly(0, 1, 2, 1, 0);
}
use of io.engineblock.activityapi.cyclelog.buffers.results.CycleResultsSegment in project engineblock by engineblock.
the class ReorderingConcurrentResultBuffer method onCycleResultSegment.
@Override
public synchronized void onCycleResultSegment(CycleResultsSegment segment) {
if (resultFilter != null) {
segment = segment.filter(resultFilter);
}
if (!(segment instanceof CanSortCycles)) {
segment = new CycleResultArray(segment);
}
((CanSortCycles) segment).sort();
segments.add(segment);
segmentCount++;
currentCount += segment.getCount();
if (currentCount >= threshold) {
logger.trace("Reordering threshold met: " + currentCount + "/" + threshold + ", sorting and pushing. (" + segments.size() + " segments)");
Collections.sort(segments);
while (currentCount >= threshold) {
CycleResultsSegment head = segments.removeFirst();
downstream.onCycleResultSegment(head);
segmentCount--;
currentCount -= head.getCount();
}
}
}
Aggregations