Search in sources :

Example 1 with CycleSegment

use of io.engineblock.activityapi.cyclelog.buffers.cycles.CycleSegment in project engineblock by engineblock.

the class BlockingSegmentInput method getInputSegment.

@Override
public synchronized CycleSegment getInputSegment(int segmentLength) {
    try {
        this.wait();
    } catch (InterruptedException ignored) {
    }
    CycleSegment toReturn = this.segment;
    this.segment = null;
    return toReturn;
}
Also used : CycleSegment(io.engineblock.activityapi.cyclelog.buffers.cycles.CycleSegment)

Example 2 with CycleSegment

use of io.engineblock.activityapi.cyclelog.buffers.cycles.CycleSegment in project engineblock by engineblock.

the class CycleArrayBufferTest method testBasicBuffering.

@Test
public void testBasicBuffering() {
    CycleArrayBuffer b = new CycleArrayBuffer(3);
    assertThat(b.remaining()).isEqualTo(3);
    b.append(4L);
    assertThat(b.remaining()).isEqualTo(2);
    b.append(7L);
    assertThat(b.remaining()).isEqualTo(1);
    b.append(2L);
    assertThat(b.remaining()).isEqualTo(0);
    CycleArray a = b.getCycleArray();
    CycleSegment is = a.getInputSegment(3);
    long[] longs = is.nextCycles(3);
    assertThat(longs).containsExactly(4L, 7L, 2L);
    assertThat(a.getInputSegment(1)).isNull();
}
Also used : CycleArray(io.engineblock.activityapi.cyclelog.buffers.cycles.CycleArray) CycleSegment(io.engineblock.activityapi.cyclelog.buffers.cycles.CycleSegment) Test(org.testng.annotations.Test)

Example 3 with CycleSegment

use of io.engineblock.activityapi.cyclelog.buffers.cycles.CycleSegment in project engineblock by engineblock.

the class InputIntervalTest method testBasicInterval.

@Test
public void testBasicInterval() {
    InputInterval ii = new InputInterval(3, 6);
    CycleSegment s1 = ii.getInputSegment(1);
    assertThat(s1).isNotNull();
    assertThat(s1.isExhausted()).isFalse();
    long v1 = s1.nextCycle();
    assertThat(v1).isEqualTo(3);
    CycleSegment s2 = ii.getInputSegment(2);
    assertThat(s2).isNotNull();
    assertThat(s2.isExhausted()).isFalse();
    long v2 = s2.nextCycle();
    assertThat(v2).isEqualTo(4);
    assertThat(s2.isExhausted()).isFalse();
    long v3 = s2.nextCycle();
    assertThat(v3).isEqualTo(5);
    assertThat(s2.isExhausted()).isTrue();
    long v4 = s2.nextCycle();
    assertThat(v4).isLessThan(0);
    assertThat(ii.getInputSegment(1)).isNull();
}
Also used : CycleSegment(io.engineblock.activityapi.cyclelog.buffers.cycles.CycleSegment) Test(org.testng.annotations.Test)

Example 4 with CycleSegment

use of io.engineblock.activityapi.cyclelog.buffers.cycles.CycleSegment 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;
    }
}
Also used : Timer(com.codahale.metrics.Timer) CycleResultSegmentBuffer(io.engineblock.activityapi.cyclelog.buffers.results.CycleResultSegmentBuffer) CycleResultsSegment(io.engineblock.activityapi.cyclelog.buffers.results.CycleResultsSegment) CycleSegment(io.engineblock.activityapi.cyclelog.buffers.cycles.CycleSegment)

Example 5 with CycleSegment

use of io.engineblock.activityapi.cyclelog.buffers.cycles.CycleSegment in project engineblock by engineblock.

the class CycleLogInputTest method testReader.

@Test
public void testReader() {
    CycleLogInput cycleLogInput = new CycleLogInput(cyclefile.getPath());
    CycleSegment i1;
    long c;
    i1 = cycleLogInput.getInputSegment(1);
    c = i1.nextCycle();
    assertThat(c).isEqualTo(1L);
    i1 = cycleLogInput.getInputSegment(1);
    c = i1.nextCycle();
    assertThat(c).isEqualTo(2L);
    i1 = cycleLogInput.getInputSegment(1);
    c = i1.nextCycle();
    assertThat(c).isEqualTo(3L);
    i1 = cycleLogInput.getInputSegment(1);
    c = i1.nextCycle();
    assertThat(c).isEqualTo(4L);
    i1 = cycleLogInput.getInputSegment(1);
    c = i1.nextCycle();
    assertThat(c).isEqualTo(5L);
    assertThat(i1.isExhausted()).isTrue();
}
Also used : CycleSegment(io.engineblock.activityapi.cyclelog.buffers.cycles.CycleSegment) Test(org.testng.annotations.Test)

Aggregations

CycleSegment (io.engineblock.activityapi.cyclelog.buffers.cycles.CycleSegment)6 Test (org.testng.annotations.Test)4 CycleArray (io.engineblock.activityapi.cyclelog.buffers.cycles.CycleArray)2 Timer (com.codahale.metrics.Timer)1 CycleResultSegmentBuffer (io.engineblock.activityapi.cyclelog.buffers.results.CycleResultSegmentBuffer)1 CycleResultsSegment (io.engineblock.activityapi.cyclelog.buffers.results.CycleResultsSegment)1