Search in sources :

Example 1 with DefaultPageCursorTracerSupplier

use of org.neo4j.io.pagecache.tracing.cursor.DefaultPageCursorTracerSupplier in project neo4j by neo4j.

the class PageCacheTest method tracerMustBeNotifiedAboutPinUnpinFaultAndEvictEventsWhenReading.

@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void tracerMustBeNotifiedAboutPinUnpinFaultAndEvictEventsWhenReading() throws IOException {
    DefaultPageCacheTracer tracer = new DefaultPageCacheTracer();
    generateFileWithRecords(file("a"), recordCount, recordSize);
    DefaultPageCursorTracerSupplier cursorTracerSupplier = DefaultPageCursorTracerSupplier.INSTANCE;
    getPageCache(fs, maxPages, pageCachePageSize, tracer, cursorTracerSupplier);
    long countedPages = 0;
    long countedFaults = 0;
    try (PagedFile pagedFile = pageCache.map(file("a"), filePageSize);
        PageCursor cursor = pagedFile.io(0, PF_SHARED_READ_LOCK)) {
        while (cursor.next()) {
            countedPages++;
            countedFaults++;
        }
        // Using next( pageId ) to the already-pinned page id does not count,
        // so we only increment once for this section
        countedPages++;
        for (int i = 0; i < 20; i++) {
            assertTrue(cursor.next(1));
        }
        // then it counts
        for (int i = 0; i < 20; i++) {
            assertTrue(cursor.next(i));
            countedPages++;
        }
    }
    cursorTracerSupplier.get().reportEvents();
    assertThat("wrong count of pins", tracer.pins(), is(countedPages));
    assertThat("wrong count of unpins", tracer.unpins(), is(countedPages));
    // We might be unlucky and fault in the second next call, on the page
    // we brought up in the first next call. That's why we assert that we
    // have observed *at least* the countedPages number of faults.
    long faults = tracer.faults();
    long bytesRead = tracer.bytesRead();
    assertThat("wrong count of faults", faults, greaterThanOrEqualTo(countedFaults));
    assertThat("wrong number of bytes read", bytesRead, greaterThanOrEqualTo(countedFaults * filePageSize));
    // Every page we move forward can put the freelist behind so the cache
    // wants to evict more pages. Plus, every page fault we do could also
    // block and get a page directly transferred to it, and these kinds of
    // evictions can count in addition to the evictions we do when the
    // cache is behind on keeping the freelist full.
    assertThat("wrong count of evictions", tracer.evictions(), both(greaterThanOrEqualTo(countedFaults - maxPages)).and(lessThanOrEqualTo(countedPages + faults)));
}
Also used : AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) DefaultPageCursorTracerSupplier(org.neo4j.io.pagecache.tracing.cursor.DefaultPageCursorTracerSupplier) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) Test(org.junit.Test)

Example 2 with DefaultPageCursorTracerSupplier

use of org.neo4j.io.pagecache.tracing.cursor.DefaultPageCursorTracerSupplier in project neo4j by neo4j.

the class PageCacheTest method tracerMustBeNotifiedAboutPinUnpinFaultFlushAndEvictionEventsWhenWriting.

@Test(timeout = SHORT_TIMEOUT_MILLIS)
public void tracerMustBeNotifiedAboutPinUnpinFaultFlushAndEvictionEventsWhenWriting() throws IOException {
    long pagesToGenerate = 142;
    DefaultPageCacheTracer tracer = new DefaultPageCacheTracer();
    DefaultPageCursorTracerSupplier tracerSupplier = DefaultPageCursorTracerSupplier.INSTANCE;
    getPageCache(fs, maxPages, pageCachePageSize, tracer, tracerSupplier);
    try (PagedFile pagedFile = pageCache.map(file("a"), filePageSize);
        PageCursor cursor = pagedFile.io(0, PF_SHARED_WRITE_LOCK)) {
        for (long i = 0; i < pagesToGenerate; i++) {
            assertTrue(cursor.next());
            assertThat(cursor.getCurrentPageId(), is(i));
            // This does not count as a pin
            assertTrue(cursor.next(i));
            assertThat(cursor.getCurrentPageId(), is(i));
            writeRecords(cursor);
        }
        // This counts as a single pin
        assertTrue(cursor.next(0));
        assertTrue(cursor.next(0));
    }
    tracerSupplier.get().reportEvents();
    assertThat("wrong count of pins", tracer.pins(), is(pagesToGenerate + 1));
    assertThat("wrong count of unpins", tracer.unpins(), is(pagesToGenerate + 1));
    // We might be unlucky and fault in the second next call, on the page
    // we brought up in the first next call. That's why we assert that we
    // have observed *at least* the countedPages number of faults.
    long faults = tracer.faults();
    assertThat("wrong count of faults", faults, greaterThanOrEqualTo(pagesToGenerate));
    // Every page we move forward can put the freelist behind so the cache
    // wants to evict more pages. Plus, every page fault we do could also
    // block and get a page directly transferred to it, and these kinds of
    // evictions can count in addition to the evictions we do when the
    // cache is behind on keeping the freelist full.
    assertThat("wrong count of evictions", tracer.evictions(), both(greaterThanOrEqualTo(pagesToGenerate - maxPages)).and(lessThanOrEqualTo(pagesToGenerate + faults)));
    // We use greaterThanOrEqualTo because we visit each page twice, and
    // that leaves a small window wherein we can race with eviction, have
    // the evictor flush the page, and then fault it back and mark it as
    // dirty again.
    // We also subtract 'maxPages' from the expected flush count, because
    // vectored IO may coalesce all the flushes we do as part of unmapping
    // the file, into a single flush.
    long flushes = tracer.flushes();
    long bytesWritten = tracer.bytesWritten();
    assertThat("wrong count of flushes", flushes, greaterThanOrEqualTo(pagesToGenerate - maxPages));
    assertThat("wrong count of bytes written", bytesWritten, greaterThanOrEqualTo(pagesToGenerate * filePageSize));
}
Also used : AdversarialPagedFile(org.neo4j.adversaries.pagecache.AdversarialPagedFile) DefaultPageCursorTracerSupplier(org.neo4j.io.pagecache.tracing.cursor.DefaultPageCursorTracerSupplier) DefaultPageCacheTracer(org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)2 AdversarialPagedFile (org.neo4j.adversaries.pagecache.AdversarialPagedFile)2 DefaultPageCacheTracer (org.neo4j.io.pagecache.tracing.DefaultPageCacheTracer)2 DefaultPageCursorTracerSupplier (org.neo4j.io.pagecache.tracing.cursor.DefaultPageCursorTracerSupplier)2