Java Code Examples for net.openhft.chronicle.core.Jvm#pause()

The following examples show how to use net.openhft.chronicle.core.Jvm#pause() . 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: Queue28Test.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    File dir = getTmpDir();
    try (final ChronicleQueue queue = SingleChronicleQueueBuilder.builder(dir, wireType)
            .testBlockSize()
            .build()) {

        final ExcerptTailer tailer = queue.createTailer();
        assertFalse(tailer.readDocument(r -> r.read(TestKey.test).int32()));

        final ExcerptAppender appender = queue.acquireAppender();
        appender.writeDocument(w -> w.write(TestKey.test).int32(1));
        Jvm.pause(100);
        assertTrue(tailer.readDocument(r -> r.read(TestKey.test).int32()));
    }
}
 
Example 2
Source File: RollCycleMultiThreadStressTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("resource")
@Override
public Throwable call() {
    ChronicleQueue queue0 = null;
    try (ChronicleQueue queue = queueBuilder(path).build()) {
        queue0 = queue;
        ExcerptAppender appender = queue.acquireAppender();
        System.out.println("Starting pretoucher");
        while (!Thread.currentThread().isInterrupted() && !queue.isClosed()) {
            Jvm.pause(50);
            appender.pretouch();
        }
    } catch (Throwable e) {
        if (queue0 != null && queue0.isClosed())
            return null;
        exception = e;
        return e;
    }
    return null;
}
 
Example 3
Source File: ContendedWriterTest.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
public StartAndMonitor(ChronicleQueue queue, String name, int writePauseMs, int sleepBetweenMillis) {
            final SlowToSerialiseAndDeserialise object = new SlowToSerialiseAndDeserialise(writePauseMs);
            Thread thread = new Thread(() -> {
                try {
                    while (running.get()) {
                        long loopStart = System.nanoTime();
                        final ExcerptAppender appender = queue.acquireAppender();
//                        System.out.println("about to open");
                        try (final DocumentContext ctx = appender.writingDocument()) {
//                            System.out.println("about to write");
                            ctx.wire().getValueOut().marshallable(object);
//                            System.out.println("about to close");
                        }
//                        System.out.println("closed");
                        long timeTaken = System.nanoTime() - loopStart;
                        histo.sampleNanos(timeTaken);
                        Jvm.pause(sleepBetweenMillis);
                    }
                } catch (Throwable t) {
                    t.printStackTrace();
                }
            }, name);
            thread.start();
        }
 
Example 4
Source File: Builder.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
public static void waitTillEqual(Map map1, Map map2, int timeOutMs) {
    int numberOfTimesTheSame = 0;
    long startTime = System.currentTimeMillis();
    for (int t = 0; t < timeOutMs + 100; t++) {
        // not map1.equals(map2), the reason is described above
        if (map1.equals(map2)) {
            numberOfTimesTheSame++;
            Jvm.pause(1);
            if (numberOfTimesTheSame == 10) {
                System.out.println("same");
                break;
            }
        }
        Jvm.pause(1);
        if (System.currentTimeMillis() - startTime > timeOutMs)
            break;
    }
}
 
Example 5
Source File: SingleChronicleQueue.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
private void waitForTheHeaderToBeBuilt(@NotNull Bytes bytes) throws IOException {
    for (int i = 0; i < 1000; i++) {
        long magic = bytes.readVolatileLong(MAGIC_OFFSET);
        if (magic == BUILDING) {
            try {
                Jvm.pause(10);
            } catch (InterruptedException e) {
                throw new IOException("Interrupted waiting for the header to be built");
            }
        } else if (magic == QUEUE_CREATED) {
            return;

        } else {
            throw new AssertionError("Invalid magic number " + Long.toHexString(magic) + " in file " + name());
        }
    }
    throw new AssertionError("Timeout waiting to build the file " + name());
}
 
Example 6
Source File: ChronicleReader.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
private void moveToSpecifiedPosition(final ChronicleQueue ic, final ExcerptTailer tailer, final boolean isFirstIteration) {
    if (isSet(startIndex) && isFirstIteration) {
        if (startIndex < ic.firstIndex()) {
            throw new IllegalArgumentException(String.format("startIndex %d is less than first index %d",
                    startIndex, ic.firstIndex()));
        }

        messageSink.accept("Waiting for startIndex " + startIndex);
        while (!tailer.moveToIndex(startIndex)) {
            Jvm.pause(100);
        }
    }

    if (isSet(maxHistoryRecords) && isFirstIteration) {
        tailer.toEnd();
        tailer.moveToIndex(Math.max(ic.firstIndex(), tailer.index() - maxHistoryRecords));
    } else if (tailInputSource && isFirstIteration) {
        tailer.toEnd();
    }
}
 
Example 7
Source File: OutputMain.java    From Chronicle-Queue-Demo with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    String path = "queue";
    SingleChronicleQueue queue = SingleChronicleQueueBuilder.binary(path).build();
    ExcerptTailer tailer = queue.createTailer();

    while (true) {
        String text = tailer.readText();
        if (text == null)
            Jvm.pause(10);
        else
            System.out.println(text);

    }
}
 
Example 8
Source File: TcpChannelHub.java    From Chronicle-Network with Apache License 2.0 5 votes vote down vote up
/**
 * called when we are finished with using the TcpChannelHub
 */
@Override
protected void performClose() {
    boolean sentClose = false;

    if (shouldSendCloseMessage) {
        try {
            eventLoop.addHandler(new SendCloseEventHandler());
            sentClose = true;
        } catch (IllegalStateException e) {
            // expected
        }
    }

    tcpSocketConsumer.prepareToShutdown();

    if (sentClose) {
        awaitAckOfClosedMessage();
    }

    if (DEBUG_ENABLED)
        Jvm.debug().on(TcpChannelHub.class, "closing connection to " + socketAddressSupplier);
    tcpSocketConsumer.stop();

    for (int i = 1; clientChannel != null; i++) {
        if (DEBUG_ENABLED) {
            Jvm.debug().on(TcpChannelHub.class, "waiting for disconnect to " + socketAddressSupplier);
        }
        Jvm.pause(Math.min(100, i));
    }

    outWire.bytes().releaseLast();
    handShakingWire.bytes().releaseLast();
}
 
Example 9
Source File: CHMLatencyTestMain.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    while (running && !Thread.currentThread().isInterrupted()) {
        Jvm.pause(1);
        long delay = System.nanoTime() - sample;
        if (delay > 1000 * 1000) {
            System.out.println("\n" + (System.currentTimeMillis() - START_TIME) + " : Delay of " + delay / 100000 / 10.0 + " ms.");
            int count = 0;
            for (StackTraceElement ste : thread.getStackTrace()) {
                System.out.println("\tat " + ste);
                if (count++ > 6) break;
            }
        }
    }
}
 
Example 10
Source File: QueueTestCommon.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
public void checkExceptions() {
    // find any discarded resources.
    System.gc();
    Jvm.pause(10);

    for (Map.Entry<Predicate<ExceptionKey>, String> expectedException : expectedExceptions.entrySet()) {
        if (!exceptions.keySet().removeIf(expectedException.getKey()))
            Slf4jExceptionHandler.WARN.on(getClass(), "No error for " + expectedException.getValue());
    }
    if (Jvm.hasException(exceptions)) {
        Jvm.dumpException(exceptions);
        Jvm.resetExceptionHandlers();
        Assert.fail();
    }
}
 
Example 11
Source File: CHMUseCasesTest.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
/**
 * * waits until map1 and map2 show the same value
 *
 * @param timeOutMs timeout in milliseconds
 */
private void waitTillEqual(final int timeOutMs) {
    int t = 0;
    for (; t < timeOutMs; t++) {
        if (map1.equals(map2))
            break;
        Jvm.pause(1);
    }
}
 
Example 12
Source File: ContendedWriterTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Override
public void writeMarshallable(@NotNull WireOut wire) {
    ValueOut valueOut = wire.getValueOut();
    for (int i = 0; i < NUMBER_OF_LONGS; i++)
        valueOut.int64(i);
    Jvm.pause(writePauseMs);
}
 
Example 13
Source File: SingleChronicleQueue.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
private void pauseUnderload() {
    // when mapping and unmapping sections really fast it appears the OS/CPU gets confused as to whether memory is valid.
    long now = System.currentTimeMillis();
    if (now - lastTimeMapped < 5)
        Jvm.pause(2);
    lastTimeMapped = now;
}
 
Example 14
Source File: RollCycleMultiThreadStressTest.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
@Override
public Throwable call() {
    ChronicleQueue queue = writerQueue(path);
    try (final ExcerptAppender appender = queue.acquireAppender()) {
        Jvm.pause(random.nextInt(DELAY_WRITER_RANDOM_MS));
        final long startTime = System.nanoTime();
        int loopIteration = 0;
        while (true) {
            final int value = write(appender);

            while (System.nanoTime() < (startTime + (loopIteration * SLEEP_PER_WRITE_NANOS))) {
                // spin
            }
            loopIteration++;

            if (value >= expectedNumberOfMessages) {
                return null;
            }
        }
    } catch (Throwable e) {
        exception = e;
        return e;
    } finally {
        if (queue != sharedWriterQueue)
            queue.close();
    }
}
 
Example 15
Source File: SingleChronicleQueueTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test
public void testReadWritingWithTimeProvider() {
    final File dir = DirectoryUtils.tempDir(testName.getMethodName());

    long time = System.currentTimeMillis();

    SetTimeProvider timeProvider = new SetTimeProvider();
    timeProvider.currentTimeMillis(time);
    try (ChronicleQueue q1 = binary(dir)
            .timeProvider(timeProvider)
            .build()) {

        try (ChronicleQueue q2 = binary(dir)
                .timeProvider(timeProvider)
                .build();

             final ExcerptAppender appender2 = q2.acquireAppender();
             final ExcerptTailer tailer1 = q1.createTailer();
             final ExcerptTailer tailer2 = q2.createTailer()) {

            try (final DocumentContext dc = appender2.writingDocument()) {
                dc.wire().write().text("some data");
            }

            try (DocumentContext dc = tailer2.readingDocument()) {
                assertTrue(dc.isPresent());
            }

            assertEquals(q1.file(), q2.file());
            // this is required for queue to re-request last/first cycle
            timeProvider.advanceMillis(1);

            for (int i = 0; i < 10; i++) {
                try (DocumentContext dc = tailer1.readingDocument()) {
                    if (dc.isPresent())
                        return;
                }
                Jvm.pause(1);
            }
            fail();
        }
    }
}
 
Example 16
Source File: SignAndVerifyPerfMain.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    final String SIGN_PRIVATE = "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd";

    Bytes<?> publicKey = Bytes.allocateDirect(32);
    Bytes<?> secretKey = Bytes.allocateDirect(64);
    BytesStore<?, ?> privateKey = fromHex(SIGN_PRIVATE);
    Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey);
    assertEquals(32, publicKey.readRemaining());
    assertEquals(64, secretKey.readRemaining());

    ThreadLocal<Bytes<?>> sigAndMsg = ThreadLocal.withInitial(() -> Bytes.allocateDirect(64 + 64));
    ThreadLocal<Bytes<?>> sigAndMsg2 = ThreadLocal.withInitial(() -> Bytes.allocateDirect(64 + 64));

    int procs = Runtime.getRuntime().availableProcessors();
    for (int t = 0; t < 10; t++) {
        int runs = procs * 20;
        long start = System.nanoTime();
        IntStream.range(0, runs).parallel().forEach(i -> {
            sigAndMsg.get().writePosition(0);
            Ed25519.sign(sigAndMsg.get(), secretKey, secretKey);
            sigAndMsg2.get().writePosition(0);
            Ed25519.sign(sigAndMsg2.get(), privateKey, secretKey);
        });
        long time = System.nanoTime() - start;

        Bytes<?> bytes = sigAndMsg.get();
        bytes.writePosition(0);
        Bytes<?> bytes2 = sigAndMsg2.get();
        bytes2.writePosition(0);
        Ed25519.sign(bytes, secretKey, secretKey);
        Ed25519.sign(bytes2, privateKey, secretKey);
        long start2 = System.nanoTime();
        IntStream.range(0, runs).parallel().forEach(i -> {
            assertTrue(Ed25519.verify(bytes, publicKey));
            assertTrue(Ed25519.verify(bytes2, publicKey));
        });
        long time2 = System.nanoTime() - start2;
        System.out.println(
                "Throughput: " + "Sign: " + (long) ((2 * runs * 1e9) / time) + "/s, " + "Verify: " + (long) ((2 * runs * 1e9) / time2) + "/s");
        Jvm.pause(100);
    }
}
 
Example 17
Source File: ForEachSegmentTest.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Test
public void stressTest() throws IOException, InterruptedException {
    ChronicleMapBuilder<Integer, MyDto> builder = ChronicleMapBuilder.simpleMapOf(Integer.class, MyDto.class)
            .entries(256)
            .actualSegments(1);
    File tmp = new File(OS.TMP, "stressTest-" + System.nanoTime());
    Thread t = null;
    try (ChronicleMap<Integer, MyDto> map = builder.createOrRecoverPersistedTo(tmp)) {
        try {
            t = new Thread(() -> {
                try {
                    for (int i = 0; i < 100; i++) {
                        System.out.println("put " + i);
                        map.put(i, new MyDto());
                        Thread.sleep(10);
                    }
                } catch (InterruptedException e) {
                    System.out.println("Interrupted");
                }
            });
            t.setDaemon(true);
            t.start();
            Jvm.pause(100);
            try (MapSegmentContext<Integer, MyDto, ?> context = map.segmentContext(0)) {
                context.forEachSegmentEntryWhile(e -> {
                    System.out.println(e.key().get() + " = " + e.value().get());
                    Jvm.pause(10);
                    return true;
                });
            }
            System.out.println("Done");
            Jvm.pause(100);
        } catch (Throwable th) {
            th.printStackTrace();
        } finally {
            if (t != null) {
                t.interrupt();
                t.join();
            }
        }
    }
}
 
Example 18
Source File: MemoryLeaksTest.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 10_000)
public void testChronicleMapCollectedAndDirectMemoryReleased()
        throws IOException, InterruptedException {
    assertFalse(OS.isMacOSX());
    // This test is flaky in Linux and Mac OS apparently because some native memory from
    // running previous/concurrent tests is released during this test, that infers with
    // the (*) check below. The aim of this test is to check that native memory is not
    // leaked and it is proven if it succeeds at least sometimes at least in some OSes.
    // This tests is successful always in Windows and successful in Linux and OS X when run
    // alone, rather than along all other Chronicle Map's tests.

    System.gc();
    Jvm.pause(100);

    long nativeMemoryUsedBeforeMap = nativeMemoryUsed();
    int serializersBeforeMap = serializerCount.get();
    // the purpose of the test is to find maps which are not closed properly.
    ChronicleMap<IntValue, String> map = getMap();
    long expectedNativeMemory = nativeMemoryUsedBeforeMap + map.offHeapMemoryUsed();
    assertEquals(expectedNativeMemory, nativeMemoryUsed());
    tryCloseFromContext(map);
    WeakReference<ChronicleMap<IntValue, String>> ref = new WeakReference<>(map);
    Assert.assertNotNull(ref.get());
    //noinspection UnusedAssignment
    map = null;

    // Wait until Map is collected by GC
    // Wait until Cleaner is called and memory is returned to the system
    for (int i = 1; i <= 10; i++) {
        if (ref.get() == null &&
                nativeMemoryUsedBeforeMap >= nativeMemoryUsed() && // (*)
                serializerCount.get() == serializersBeforeMap) {
            break;
        }
        System.gc();
        Jvm.pause(i * 10);
        System.out.println("ref.get()=" + (ref.get() == null));
        System.out.println(nativeMemoryUsedBeforeMap + " <=> " + nativeMemoryUsed());
        System.out.println(serializerCount.get() + " <=> " + serializersBeforeMap);
    }
    if (nativeMemoryUsedBeforeMap < nativeMemoryUsed())
        Assert.assertEquals(nativeMemoryUsedBeforeMap, nativeMemoryUsed());
    Assert.assertEquals(serializersBeforeMap, serializerCount.get());
}
 
Example 19
Source File: ProcessInstanceLimiter.java    From Chronicle-Map with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
    ProcessInstanceLimiter.limitTo(2);
    Jvm.pause(60L * 1000L);
}
 
Example 20
Source File: MultiThreadedRollTest.java    From Chronicle-Queue with Apache License 2.0 4 votes vote down vote up
@Test(timeout = 10000)
public void test() throws ExecutionException, InterruptedException {

    final SetTimeProvider timeProvider = new SetTimeProvider();
    timeProvider.currentTimeMillis(1000);
    final File path = DirectoryUtils.tempDir("MultiThreadedRollTest");

    try (final ChronicleQueue wqueue = binary(path)
            .testBlockSize()
            .timeProvider(timeProvider)
            .rollCycle(TEST_SECONDLY)
            .build()) {

        wqueue.acquireAppender().writeText("hello world");

        try (final ChronicleQueue rqueue = binary(path)
                .testBlockSize()
                .timeProvider(timeProvider)
                .rollCycle(TEST_SECONDLY)
                .build()) {

            ExcerptTailer tailer = rqueue.createTailer();
            Future f = reader.submit(() -> {
                long index;
                do {
                    try (DocumentContext documentContext = tailer.readingDocument()) {
                        System.out.println("tailer.state: " + tailer.state());
                        // index is only meaningful if present.
                        index = documentContext.index();
                        //    if (documentContext.isPresent())
                        final boolean present = documentContext.isPresent();
                        System.out.println("documentContext.isPresent=" + present
                                + (present ? ",index=" + Long.toHexString(index) : ", no index"));
                        Jvm.pause(50);
                    }
                } while (index != 0x200000000L && !reader.isShutdown());

            });

            timeProvider.currentTimeMillis(2000);
            ((StoreAppender) wqueue.acquireAppender())
                    .writeEndOfCycleIfRequired();
            Jvm.pause(200);
            wqueue.acquireAppender().writeText("hello world");
            f.get();
        }
    }
}