Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 3 Oct 2021 21:57:00 GMT
From:      Colin Percival <cperciva@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: 04b9b7c507c5 - main - loader bcache: Track unconsumed readahead
Message-ID:  <202110032157.193Lv0UP041376@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=04b9b7c507c52daf7b2999329a50825db098151f

commit 04b9b7c507c52daf7b2999329a50825db098151f
Author:     Colin Percival <cperciva@FreeBSD.org>
AuthorDate: 2021-10-03 21:49:41 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2021-10-03 21:54:09 +0000

    loader bcache: Track unconsumed readahead
    
    The loader bcache attempts to determine whether readahead is useful,
    increasing or decreasing its readahead length based on whether a
    read could be serviced out of the cache.  This resulted in two
    unfortunate behaviours:
    
    1. A series of consecutive 32 kB reads are requested and bcache
    performs 16 kB readaheads.  For each read, bcache determines that,
    since only the first 16 kB is already in the cache, the readahead
    was not useful, and keeps the readahead at the minimum (16 kB) level.
    
    2. A series of consecutive 32 kB reads are requested and bcache
    starts with a 32 kB readahead resulting in a 64 kB being read on
    the first request.  The second 32 kB request can be serviced out of
    the cache, and bcache responds by doubling its readahead length to
    64 kB.  The third 32 kB request cannot be serviced out of the cache,
    and bcache reduces its readahead length back down to 32 kB.
    
    The first syndrome converts a series of 32 kB reads into a series of
    (misaligned) 32 kB reads, while the second syndrome converts a series
    of 32 kB reads into a series of 64 kB reads; in both cases we do not
    increase the readahead length to its limit (currently 128 kB) no
    matter how many consecutive read requests are made.
    
    This change avoids this problem by tracking the "unconsumed
    readahead" length; readahead is deemed to be useful (and the
    read-ahead length is potentially increased) not only if a request was
    completely serviced out of the cache, but also if *any* of the request
    was serviced out of the cache and that length matches the amount of
    unconsumed readahead.  Conversely, we now only reduce the readahead
    length in cases where there was unconsumed readahead data.
    
    In my testing on an EC2 c5.xlarge instance, this change reduces the
    boot time by roughly 120 ms.
    
    Reviewed by:    imp, tsoome
    MFC after:      1 week
    Sponsored by:   https://patreon.com/cperciva
    Differential Revision:  https://reviews.freebsd.org/D32250
---
 stand/common/bcache.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/stand/common/bcache.c b/stand/common/bcache.c
index b3b8b22c7d21..b79d609b198b 100644
--- a/stand/common/bcache.c
+++ b/stand/common/bcache.c
@@ -67,6 +67,7 @@ struct bcache {
     size_t		bcache_nblks;
     size_t		ra;
     daddr_t		bcache_nextblkno;
+    size_t		ralen;
 };
 
 static u_int bcache_total_nblks;	/* set by bcache_init */
@@ -250,14 +251,23 @@ read_strategy(void *devdata, int rw, daddr_t blk, size_t size,
      * increase it when we notice that readahead was useful and decrease
      * it when we notice that readahead was not useful.
      */
-    if (complete) {
+    if (complete || (i == bc->ralen && bc->ralen > 0)) {
 	if (bc->ra < BCACHE_READAHEAD)
 	    bc->ra <<= 1;	/* increase read ahead */
     } else {
-	if (nblk - i > BCACHE_MINREADAHEAD && bc->ra > BCACHE_MINREADAHEAD)
+	if (nblk - i > BCACHE_MINREADAHEAD && bc->ralen > 0 &&
+	  bc->ra > BCACHE_MINREADAHEAD)
 	    bc->ra >>= 1;	/* reduce read ahead */
     }
 
+    /* Adjust our "unconsumed readahead" value. */
+    if (blk == bc->bcache_nextblkno) {
+	if (nblk > bc->ralen)
+	    bc->ralen = 0;
+	else
+	    bc->ralen -= nblk;
+    }
+
     if (complete) {	/* whole set was in cache, return it */
 	bcopy(bc->bcache_data + (bcache_blksize * BHASH(bc, blk)), buf, size);
 	goto done;
@@ -314,7 +324,10 @@ read_strategy(void *devdata, int rw, daddr_t blk, size_t size,
     if (ra != 0 && ra != bc->bcache_nblks) { /* do we have RA space? */
 	ra = MIN(bc->ra, ra - 1);
 	ra = rounddown(ra, 16);		/* multiple of 16 blocks */
+	bc->ralen = ra;
 	p_size += ra;
+    } else {
+	bc->ralen = 0;
     }
 
     /* invalidate bcache */



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202110032157.193Lv0UP041376>