git: 57e897ec3d49 - stable/13 - Only trigger read-ahead if two adjacent blocks have been requested.

From: Colin Percival <cperciva_at_FreeBSD.org>
Date: Wed, 29 Dec 2021 23:06:29 UTC
The branch stable/13 has been updated by cperciva:

URL: https://cgit.FreeBSD.org/src/commit/?id=57e897ec3d49664f1d9c0a6545660dda4c1e4fc0

commit 57e897ec3d49664f1d9c0a6545660dda4c1e4fc0
Author:     Maxim Sobolev <sobomax@FreeBSD.org>
AuthorDate: 2021-08-20 19:33:51 +0000
Commit:     Colin Percival <cperciva@FreeBSD.org>
CommitDate: 2021-12-29 22:53:19 +0000

    Only trigger read-ahead if two adjacent blocks have been requested.
    
    The change makes block caching algorithm to work better for remote
    media on low-BW/high-delay links.
    
    This cuts boot time over IP KVMs noticeably, since the initialization
    stage reads bunch of small 4th (and now lua) files that are not in
    the same cache stripe (usually), thus wasting lot of bandwidth and
    increasing latency even further.
    
    The original regression came in 2017 with revision 87ed2b7f5. We've
    seen increase of time it takes for the loader to get to the kernel
    loading from under a minute to 10-15 minutes in many cases.
    
    Reviewed by:    tsoome
    MFC after:      1 month
    Differential Revision:  https://reviews.freebsd.org/D31623
    
    (cherry picked from commit 0d13f5343fafbf3067ffc33a507ffca0375c4417)
---
 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 a020f3c3c53c..526f9fe3fa5c 100644
--- a/stand/common/bcache.c
+++ b/stand/common/bcache.c
@@ -66,6 +66,7 @@ struct bcache {
     caddr_t		bcache_data;
     size_t		bcache_nblks;
     size_t		ra;
+    daddr_t		bcache_nextblkno;
 };
 
 static u_int bcache_total_nblks;	/* set by bcache_init */
@@ -163,6 +164,7 @@ bcache_allocate(void)
     }
     bcache_units++;
     bc->ra = BCACHE_READAHEAD;	/* optimistic read ahead */
+    bc->bcache_nextblkno = -1;
     return (bc);
 }
 
@@ -291,6 +293,14 @@ read_strategy(void *devdata, int rw, daddr_t blk, size_t size,
     else
 	ra = bc->bcache_nblks - BHASH(bc, p_blk + p_size);
 
+    /*
+     * Only trigger read-ahead if we detect two blocks being read
+     * sequentially.
+     */
+    if ((bc->bcache_nextblkno != blk) && ra != 0) {
+        ra = 0;
+    }
+
     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 */
@@ -342,8 +352,11 @@ read_strategy(void *devdata, int rw, daddr_t blk, size_t size,
     }
 
  done:
-    if ((result == 0) && (rsize != NULL))
-	*rsize = size;
+    if (result == 0) {
+        if (rsize != NULL)
+	    *rsize = size;
+        bc->bcache_nextblkno = blk + (size / DEV_BSIZE);
+    }
     return(result);
 }