Search in sources :

Example 6 with DirectMemoryRegion

use of org.apache.ignite.internal.mem.DirectMemoryRegion in project ignite by apache.

the class PageIdDistributionTest method _testRealHistory.

/**
 * Uncomment and run this test manually to get data to plot histogram for per-element distance from ideal.
 * You can use Octave to plot the histogram:
 * <pre>
 *     all = csvread("histo.txt");
 *     hist(all, 200)
 * </pre>
 *
 * @throws Exception If failed.
 */
public void _testRealHistory() throws Exception {
    int capacity = CACHE_IDS.length * PARTS * PAGES;
    info("Capacity: " + capacity);
    long mem = FullPageIdTable.requiredMemory(capacity);
    info(U.readableSize(mem, true));
    UnsafeMemoryProvider prov = new UnsafeMemoryProvider(new JavaLogger());
    prov.initialize(new long[] { mem });
    DirectMemoryRegion region = prov.nextRegion();
    try {
        long seed = U.currentTimeMillis();
        info("Seed: " + seed + "L; //");
        Random rnd = new Random(seed);
        FullPageIdTable tbl = new FullPageIdTable(region.address(), region.size(), true);
        Map<T2<Integer, Integer>, Integer> allocated = new HashMap<>();
        for (int i = 0; i < capacity; i++) {
            int cacheId = CACHE_IDS[rnd.nextInt(CACHE_IDS.length)];
            int partId = rnd.nextInt(PARTS);
            T2<Integer, Integer> key = new T2<>(cacheId, partId);
            Integer pageIdx = allocated.get(key);
            pageIdx = pageIdx == null ? 1 : pageIdx + 1;
            if (pageIdx > PAGES)
                continue;
            tbl.put(cacheId, PageIdUtils.pageId(partId, (byte) 0, pageIdx), 1, 0);
            allocated.put(key, pageIdx);
            if (i > 0 && i % 100_000 == 0)
                info("Done: " + i);
        }
        int[] scans = new int[capacity];
        int cur = 0;
        for (T2<Integer, Integer> key : allocated.keySet()) {
            Integer alloc = allocated.get(key);
            if (alloc != null) {
                for (int idx = 1; idx <= alloc; idx++) {
                    scans[cur] = tbl.distanceFromIdeal(key.get1(), PageIdUtils.pageId(key.get2(), (byte) 0, idx), 0);
                    assert scans[cur] != -1;
                    cur++;
                }
            }
        }
        try (FileOutputStream out = new FileOutputStream("histo.txt")) {
            PrintWriter w = new PrintWriter(new OutputStreamWriter(out));
            for (int scan : scans) {
                if (scan != 0)
                    w.println(scan);
            }
            w.flush();
        }
    } finally {
        prov.shutdown();
    }
}
Also used : HashMap(java.util.HashMap) DirectMemoryRegion(org.apache.ignite.internal.mem.DirectMemoryRegion) FullPageIdTable(org.apache.ignite.internal.processors.cache.persistence.pagemem.FullPageIdTable) Random(java.util.Random) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) UnsafeMemoryProvider(org.apache.ignite.internal.mem.unsafe.UnsafeMemoryProvider) JavaLogger(org.apache.ignite.logger.java.JavaLogger) T2(org.apache.ignite.internal.util.typedef.T2) PrintWriter(java.io.PrintWriter)

Example 7 with DirectMemoryRegion

use of org.apache.ignite.internal.mem.DirectMemoryRegion in project ignite by apache.

the class FullPageIdTableTest method doPutRemoveTest.

/**
 * @param seed random initial value.
 * @param newMapImpl use RobinHood map
 * @param iters iterations.
 */
private void doPutRemoveTest(long seed, boolean newMapImpl, int iters) {
    int elementsCnt = 7000;
    // System.setProperty(IGNITE_LONG_LONG_HASH_MAP_LOAD_FACTOR, "11");
    long mem = newMapImpl ? RobinHoodBackwardShiftHashMap.requiredMemory(elementsCnt) : FullPageIdTable.requiredMemory(elementsCnt);
    DirectMemoryProvider prov = new UnsafeMemoryProvider(log);
    prov.initialize(new long[] { mem });
    DirectMemoryRegion region = prov.nextRegion();
    try {
        info("Seed: " + seed + "L; //");
        Random rnd = new Random(seed);
        LoadedPagesMap tbl = newMapImpl ? new RobinHoodBackwardShiftHashMap(region.address(), region.size()) : new FullPageIdTable(region.address(), region.size(), true);
        Map<FullPageId, Long> check = new HashMap<>();
        int tag = 0;
        for (int i = 0; i < iters; i++) {
            int op = rnd.nextInt(5);
            int cacheId = rnd.nextInt(CACHE_ID_RANGE2) + 1;
            int pageId = rnd.nextInt(PAGE_ID_RANGE2);
            FullPageId fullId = new FullPageId(pageId, cacheId);
            if (op == 0) {
                long val = tbl.get(cacheId, pageId, tag, -1, -2);
                if (val == -2)
                    tbl.refresh(cacheId, pageId, tag);
                else {
                    Long checkVal = check.get(fullId);
                    if (checkVal != null) {
                        assertEquals("Ret " + val + "Check " + checkVal, checkVal.longValue(), val);
                    }
                }
            } else if ((op == 1 || op == 2) && (check.size() < elementsCnt)) {
                long val = U.safeAbs(rnd.nextLong());
                tbl.put(cacheId, pageId, val, tag);
                check.put(fullId, val);
            } else if ((op == 3) && check.size() >= elementsCnt * 2 / 3) {
                tbl.remove(cacheId, pageId);
                check.remove(fullId);
            } else if (check.size() >= elementsCnt * 2 / 3) {
                int idx = rnd.nextInt(tbl.capacity());
                ReplaceCandidate ec = tbl.getNearestAt(idx);
                if (ec != null) {
                    FullPageId fullPageId = ec.fullId();
                    tbl.remove(fullPageId.groupId(), fullPageId.pageId());
                    check.remove(fullPageId);
                }
            }
            if (i > 0 && i % 100_000 == 0) {
                info("Done: " + i + " Size: " + check.size() + " Capacity: " + tbl.capacity());
                verifyLinear(tbl, check);
                tag++;
            }
            i++;
        }
        verifyLinear(tbl, check);
    } finally {
        long msPassed = U.currentTimeMillis() - seed;
        System.err.println("Seed used [" + seed + "] duration [" + msPassed + "] ms");
        prov.shutdown();
    }
}
Also used : HashMap(java.util.HashMap) DirectMemoryRegion(org.apache.ignite.internal.mem.DirectMemoryRegion) Random(java.util.Random) DirectMemoryProvider(org.apache.ignite.internal.mem.DirectMemoryProvider) UnsafeMemoryProvider(org.apache.ignite.internal.mem.unsafe.UnsafeMemoryProvider) FullPageId(org.apache.ignite.internal.pagemem.FullPageId)

Aggregations

DirectMemoryRegion (org.apache.ignite.internal.mem.DirectMemoryRegion)7 HashMap (java.util.HashMap)3 Random (java.util.Random)3 UnsafeMemoryProvider (org.apache.ignite.internal.mem.unsafe.UnsafeMemoryProvider)3 FullPageId (org.apache.ignite.internal.pagemem.FullPageId)2 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 PrintWriter (java.io.PrintWriter)1 ArrayList (java.util.ArrayList)1 IgniteException (org.apache.ignite.IgniteException)1 DirectMemoryProvider (org.apache.ignite.internal.mem.DirectMemoryProvider)1 UnsafeChunk (org.apache.ignite.internal.mem.UnsafeChunk)1 FullPageIdTable (org.apache.ignite.internal.processors.cache.persistence.pagemem.FullPageIdTable)1 T2 (org.apache.ignite.internal.util.typedef.T2)1 JavaLogger (org.apache.ignite.logger.java.JavaLogger)1 Test (org.junit.Test)1