Search in sources :

Example 1 with DirectMemoryRegion

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

the class FullPageIdTableTest method testRandomOperations.

/**
 * @throws Exception if failed.
 */
@Test
public void testRandomOperations() throws Exception {
    int cnt = CACHE_ID_RANGE * PAGE_ID_RANGE;
    long mem = FullPageIdTable.requiredMemory(cnt);
    UnsafeMemoryProvider prov = new UnsafeMemoryProvider(log);
    prov.initialize(new long[] { mem });
    DirectMemoryRegion region = prov.nextRegion();
    try {
        long seed = U.currentTimeMillis();
        info("Seed: " + seed + "L; //");
        Random rnd = new Random(seed);
        LoadedPagesMap tbl = new FullPageIdTable(region.address(), region.size(), true);
        Map<FullPageId, Long> check = new HashMap<>();
        for (int i = 0; i < 10_000; i++) {
            int cacheId = rnd.nextInt(CACHE_ID_RANGE) + 1;
            int pageId = rnd.nextInt(PAGE_ID_RANGE);
            FullPageId fullId = new FullPageId(pageId, cacheId);
            boolean put = rnd.nextInt(3) != -1;
            if (put) {
                long val = rnd.nextLong();
                tbl.put(cacheId, pageId, val, 0);
                check.put(fullId, val);
            } else {
                tbl.remove(cacheId, pageId);
                check.remove(fullId);
            }
            verifyLinear(tbl, check);
            if (i > 0 && i % 1000 == 0)
                info("Done: " + i);
        }
    } finally {
        prov.shutdown();
    }
}
Also used : Random(java.util.Random) HashMap(java.util.HashMap) DirectMemoryRegion(org.apache.ignite.internal.mem.DirectMemoryRegion) UnsafeMemoryProvider(org.apache.ignite.internal.mem.unsafe.UnsafeMemoryProvider) FullPageId(org.apache.ignite.internal.pagemem.FullPageId) Test(org.junit.Test)

Example 2 with DirectMemoryRegion

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

the class PageMemoryImpl method start.

/**
 * {@inheritDoc}
 */
@Override
public void start() throws IgniteException {
    directMemoryProvider.initialize(sizes);
    List<DirectMemoryRegion> regions = new ArrayList<>(sizes.length);
    while (true) {
        DirectMemoryRegion reg = directMemoryProvider.nextRegion();
        if (reg == null)
            break;
        regions.add(reg);
    }
    int regs = regions.size();
    segments = new Segment[regs - 1];
    DirectMemoryRegion cpReg = regions.get(regs - 1);
    checkpointPool = new PagePool(regs - 1, cpReg, cpBufPagesCntr);
    long checkpointBuf = cpReg.size();
    long totalAllocated = 0;
    int pages = 0;
    long totalTblSize = 0;
    for (int i = 0; i < regs - 1; i++) {
        assert i < segments.length;
        DirectMemoryRegion reg = regions.get(i);
        totalAllocated += reg.size();
        segments[i] = new Segment(i, regions.get(i), checkpointPool.pages() / segments.length, throttlingPlc);
        pages += segments[i].pages();
        totalTblSize += segments[i].tableSize();
    }
    initWriteThrottle();
    if (log.isInfoEnabled())
        log.info("Started page memory [memoryAllocated=" + U.readableSize(totalAllocated, false) + ", pages=" + pages + ", tableSize=" + U.readableSize(totalTblSize, false) + ", checkpointBuffer=" + U.readableSize(checkpointBuf, false) + ']');
}
Also used : ArrayList(java.util.ArrayList) DirectMemoryRegion(org.apache.ignite.internal.mem.DirectMemoryRegion)

Example 3 with DirectMemoryRegion

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

the class UnsafeMemoryProvider method nextRegion.

/**
 * {@inheritDoc}
 */
@Override
public DirectMemoryRegion nextRegion() {
    if (regions.size() == sizes.length)
        return null;
    long chunkSize = sizes[regions.size()];
    long ptr;
    try {
        ptr = GridUnsafe.allocateMemory(chunkSize);
    } catch (IllegalArgumentException e) {
        String msg = "Failed to allocate next memory chunk: " + U.readableSize(chunkSize, true) + ". Check if chunkSize is too large and 32-bit JVM is used.";
        if (regions.size() == 0)
            throw new IgniteException(msg, e);
        U.error(log, msg);
        return null;
    }
    if (ptr <= 0) {
        U.error(log, "Failed to allocate next memory chunk: " + U.readableSize(chunkSize, true));
        return null;
    }
    DirectMemoryRegion region = new UnsafeChunk(ptr, chunkSize);
    regions.add(region);
    return region;
}
Also used : UnsafeChunk(org.apache.ignite.internal.mem.UnsafeChunk) IgniteException(org.apache.ignite.IgniteException) DirectMemoryRegion(org.apache.ignite.internal.mem.DirectMemoryRegion)

Example 4 with DirectMemoryRegion

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

the class UnsafeMemoryProvider method shutdown.

/**
 * {@inheritDoc}
 */
@Override
public void shutdown() {
    if (regions != null) {
        for (Iterator<DirectMemoryRegion> it = regions.iterator(); it.hasNext(); ) {
            DirectMemoryRegion chunk = it.next();
            GridUnsafe.freeMemory(chunk.address());
            // Safety.
            it.remove();
        }
    }
}
Also used : DirectMemoryRegion(org.apache.ignite.internal.mem.DirectMemoryRegion)

Example 5 with DirectMemoryRegion

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

the class PageMemoryNoStoreImpl method addSegment.

/**
 * Attempts to add a new memory segment.
 *
 * @param oldRef Old segments array. If this method observes another segments array, it will allocate a new
 *      segment (if possible). If the array has already been updated, it will return the last element in the
 *      new array.
 * @return Added segment, if successfull, {@code null} if failed to add.
 */
private synchronized Segment addSegment(Segment[] oldRef) {
    if (segments == oldRef) {
        DirectMemoryRegion region = directMemoryProvider.nextRegion();
        // No more memory is available.
        if (region == null)
            return null;
        if (oldRef != null) {
            if (log.isInfoEnabled())
                log.info("Allocated next memory segment [plcName=" + dataRegionCfg.getName() + ", chunkSize=" + U.readableSize(region.size(), true) + ']');
        }
        Segment[] newRef = new Segment[oldRef == null ? 1 : oldRef.length + 1];
        if (oldRef != null)
            System.arraycopy(oldRef, 0, newRef, 0, oldRef.length);
        Segment lastSeg = oldRef == null ? null : oldRef[oldRef.length - 1];
        Segment allocated = new Segment(newRef.length - 1, region, lastSeg == null ? 0 : lastSeg.sumPages());
        allocated.init();
        newRef[newRef.length - 1] = allocated;
        segments = newRef;
    }
    // Only this synchronized method writes to segments, so it is safe to read twice.
    return segments[segments.length - 1];
}
Also used : DirectMemoryRegion(org.apache.ignite.internal.mem.DirectMemoryRegion)

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