Search in sources :

Example 6 with MemoryMA

use of io.questdb.cairo.vm.api.MemoryMA in project questdb by bluestreak01.

the class CairoMemoryTest method testSlidingWindowMemoryCannotMap.

@Test
public void testSlidingWindowMemoryCannotMap() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (Path path = new Path()) {
            path.of(temp.getRoot().getAbsolutePath());
            final int N = 100000;
            final Rnd rnd = new Rnd();
            FilesFacade ff = new FilesFacadeImpl() {

                int counter = 2;

                @Override
                public long mmap(long fd, long len, long offset, int flags, int memoryTag) {
                    if (flags == Files.MAP_RO && --counter == 0) {
                        return -1;
                    }
                    return super.mmap(fd, len, offset, flags, memoryTag);
                }
            };
            try (MemoryMA mem = Vm.getMAInstance()) {
                mem.of(ff, path.concat("x.dat").$(), ff.getPageSize(), MemoryTag.MMAP_DEFAULT);
                for (int i = 0; i < N; i++) {
                    mem.putLong(rnd.nextLong());
                }
                try (MemorySRImpl mem2 = new MemorySRImpl()) {
                    mem2.of(mem, MemoryTag.MMAP_DEFAULT);
                    try {
                        rnd.reset();
                        for (int i = 0; i < N; i++) {
                            Assert.assertEquals(rnd.nextLong(), mem2.getLong(i * 8));
                        }
                        Assert.fail();
                    } catch (CairoException e) {
                        TestUtils.assertContains(e.getMessage(), "could not mmap");
                    }
                    rnd.reset();
                    for (int i = 0; i < N; i++) {
                        Assert.assertEquals(rnd.nextLong(), mem2.getLong(i * 8));
                    }
                }
            }
        }
    });
}
Also used : Path(io.questdb.std.str.Path) MemoryMA(io.questdb.cairo.vm.api.MemoryMA) MemorySRImpl(io.questdb.cairo.vm.MemorySRImpl) Test(org.junit.Test)

Example 7 with MemoryMA

use of io.questdb.cairo.vm.api.MemoryMA in project questdb by bluestreak01.

the class CairoMemoryTest method testAppendAfterMMapFailure.

@Test
public void testAppendAfterMMapFailure() throws Exception {
    long used = Unsafe.getMemUsed();
    Rnd rnd = new Rnd();
    class X extends FilesFacadeImpl {

        boolean force = true;

        @Override
        public long mmap(long fd, long len, long offset, int flags, int memoryTag) {
            if (force || rnd.nextBoolean()) {
                force = false;
                return super.mmap(fd, len, offset, flags, memoryTag);
            } else {
                return -1;
            }
        }
    }
    X ff = new X();
    long openFileCount = ff.getOpenFileCount();
    int failureCount = 0;
    try (Path path = new Path()) {
        path.of(temp.newFile().getAbsolutePath());
        try (MemoryMA mem = Vm.getMAInstance()) {
            mem.of(ff, path.$(), ff.getPageSize() * 2, MemoryTag.MMAP_DEFAULT);
            int i = 0;
            while (i < N) {
                try {
                    mem.putLong(i);
                    i++;
                } catch (CairoException ignore) {
                    failureCount++;
                }
            }
            Assert.assertEquals(N * 8, mem.getAppendOffset());
        }
    }
    Assert.assertTrue(failureCount > 0);
    Assert.assertEquals(used, Unsafe.getMemUsed());
    Assert.assertEquals(openFileCount, ff.getOpenFileCount());
}
Also used : Path(io.questdb.std.str.Path) MemoryMA(io.questdb.cairo.vm.api.MemoryMA) Test(org.junit.Test)

Example 8 with MemoryMA

use of io.questdb.cairo.vm.api.MemoryMA in project questdb by bluestreak01.

the class MemRemappedFileTest method test.

private double test(MemoryMR readMem) {
    long nanos = 0;
    try (MemoryMA appMem = Vm.getMAInstance()) {
        for (int cycle = 0; cycle < NCYCLES; cycle++) {
            path.trimTo(0).concat(root).concat("file" + nFile).$();
            nFile++;
            Random rand = new Random(0);
            long expectedTotal = 0;
            nanos = System.nanoTime();
            long actualTotal = 0;
            long offset = 0;
            for (int nPage = 0; nPage < NPAGES; nPage++) {
                long newSize = MAPPING_PAGE_SIZE * (nPage + 1);
                appMem.of(ff, path, newSize, MemoryTag.MMAP_DEFAULT);
                appMem.skip(newSize - MAPPING_PAGE_SIZE);
                for (int i = 0; i < MAPPING_PAGE_SIZE; i++) {
                    byte b = (byte) rand.nextInt();
                    appMem.putByte(b);
                    expectedTotal += b;
                }
                if (nPage == 0) {
                    readMem.smallFile(ff, path, MemoryTag.MMAP_DEFAULT);
                } else {
                    readMem.extend(newSize);
                }
                for (int i = 0; i < MAPPING_PAGE_SIZE; i++) {
                    actualTotal += readMem.getByte(offset);
                    offset++;
                }
            }
            nanos = System.nanoTime() - nanos;
            Assert.assertEquals(expectedTotal, actualTotal);
            ff.remove(path);
        }
        readMem.close();
        return nanos / 1000000.0;
    }
}
Also used : MemoryMA(io.questdb.cairo.vm.api.MemoryMA) Random(java.util.Random)

Example 9 with MemoryMA

use of io.questdb.cairo.vm.api.MemoryMA in project questdb by bluestreak01.

the class CairoMemoryTest method testSlidingWindowMemory.

@Test
public void testSlidingWindowMemory() throws Exception {
    TestUtils.assertMemoryLeak(() -> {
        try (Path path = new Path()) {
            path.of(temp.getRoot().getAbsolutePath());
            final int N = 100000;
            final Rnd rnd = new Rnd();
            try (MemoryMA mem = Vm.getMAInstance()) {
                mem.of(FF, path.concat("x.dat").$(), FF.getPageSize(), MemoryTag.MMAP_DEFAULT);
                for (int i = 0; i < N; i++) {
                    mem.putLong(rnd.nextLong());
                }
                try (MemorySRImpl mem2 = new MemorySRImpl()) {
                    mem2.of(mem, MemoryTag.MMAP_DEFAULT);
                    // try to read outside of original page bounds
                    try {
                        mem2.getLong(N * 16);
                        Assert.fail();
                    } catch (CairoException e) {
                        TestUtils.assertContains(e.getFlyweightMessage(), "Trying to map read-only page outside");
                    }
                    // make sure jump() is reported
                    try {
                        mem2.jumpTo(1024);
                        Assert.fail();
                    } catch (UnsupportedOperationException e) {
                        TestUtils.assertContains(e.getMessage(), "Cannot jump() read-only memory");
                    }
                    rnd.reset();
                    for (int i = 0; i < N; i++) {
                        Assert.assertEquals(rnd.nextLong(), mem2.getLong(i * 8));
                    }
                }
            }
        }
    });
}
Also used : Path(io.questdb.std.str.Path) MemoryMA(io.questdb.cairo.vm.api.MemoryMA) MemorySRImpl(io.questdb.cairo.vm.MemorySRImpl) Test(org.junit.Test)

Aggregations

MemoryMA (io.questdb.cairo.vm.api.MemoryMA)9 Path (io.questdb.std.str.Path)7 Test (org.junit.Test)6 MemorySRImpl (io.questdb.cairo.vm.MemorySRImpl)2 MemoryMR (io.questdb.cairo.vm.api.MemoryMR)2 RecordMetadata (io.questdb.cairo.sql.RecordMetadata)1 MemoryARW (io.questdb.cairo.vm.api.MemoryARW)1 LPSZ (io.questdb.std.str.LPSZ)1 Random (java.util.Random)1