From owner-dev-commits-src-all@freebsd.org Sun Oct 3 21:57:01 2021 Return-Path: Delivered-To: dev-commits-src-all@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 16F2E66FCBD; Sun, 3 Oct 2021 21:57:01 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4HMyP900vwz4gkV; Sun, 3 Oct 2021 21:57:01 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id D454A5CEC; Sun, 3 Oct 2021 21:57:00 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 193Lv0kl041377; Sun, 3 Oct 2021 21:57:00 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 193Lv0UP041376; Sun, 3 Oct 2021 21:57:00 GMT (envelope-from git) Date: Sun, 3 Oct 2021 21:57:00 GMT Message-Id: <202110032157.193Lv0UP041376@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Colin Percival Subject: git: 04b9b7c507c5 - main - loader bcache: Track unconsumed readahead MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: cperciva X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 04b9b7c507c52daf7b2999329a50825db098151f Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-all@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commit messages for all branches of the src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 03 Oct 2021 21:57:01 -0000 The branch main has been updated by cperciva: URL: https://cgit.FreeBSD.org/src/commit/?id=04b9b7c507c52daf7b2999329a50825db098151f commit 04b9b7c507c52daf7b2999329a50825db098151f Author: Colin Percival AuthorDate: 2021-10-03 21:49:41 +0000 Commit: Colin Percival 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 */