From owner-svn-src-head@freebsd.org Wed Oct 14 13:29:03 2020 Return-Path: Delivered-To: svn-src-head@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 A4B44439673; Wed, 14 Oct 2020 13:29:03 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: from mail-wr1-x443.google.com (mail-wr1-x443.google.com [IPv6:2a00:1450:4864:20::443]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "smtp.gmail.com", Issuer "GTS CA 1O1" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CBCtR0JSKz404y; Wed, 14 Oct 2020 13:29:01 +0000 (UTC) (envelope-from mjguzik@gmail.com) Received: by mail-wr1-x443.google.com with SMTP id n6so3800903wrm.13; Wed, 14 Oct 2020 06:29:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=MSQBdFO4XBoyhQn8n683lLpkVRytTnZt+RXLxOhgH6A=; b=WFhwW9ThVhbGelNFLjlZfl/RJ1c0buONcTZ+4IXXzc+qjYQ9Kp52qkIhsO9cMbkaBo VO1WyhXOeNtvpI4SArn5P+F6elJfP9RgvPpC1t1aaKLWTZmzJH3xBXKmYc8cKbNwZMWM 9yxfCHUiucLJAiAdxnV8JOktWVgt5bgWTd4+jutnHcZuQtAbJEIbl66RxYv4Qkd9dhAw cmkQjYrZWDr5rY/lr7ouvkdfy6qKE5XYny5q9te3hNm4//opSKTcxKnDsOC5o7+w3F+Y gsXIS4mUNBZ9NFf37BFHUbXq9+p6VjnhAPiJrKgsaXS6szQbzeTg1SGIY6WHnT7mJTXx sH8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=MSQBdFO4XBoyhQn8n683lLpkVRytTnZt+RXLxOhgH6A=; b=QihrPmbedYDz9U9JR69C2FVjMYl1bDyYLnxC+wjOe5+Vd0Jgo2Lp3oizyM9ror/JS9 NPU2gdHRIZVlyXzprA9OKdSt16MN6bRU+JiwaWCl2c0zn6GJIPiTB7S3b/K/pZPOJZqW r1BBeirg+faC21EcOFaUtKtqv24kolcjGjLrZ9fNO/JfEc4fIHQ6Vv3YzsT+WM798Y61 hBeQ5gUJaBAOofoAoROJxp8uJA+/BEkaTgAuu8A0Uv2aYxXbs7+OL6uszcGAUuNlAITC vCJIQ0wVoQKTh3UXpuF4jtprprsDkA07HO/oViO2w/FuOAMDN5oc9ZMRywqH4VLEsF60 1fDQ== X-Gm-Message-State: AOAM5327rkEb54XuUilEfDtNjTFQY9vHgVKRups6QFaHGglKue7f/uhi kV0U37K4xbZCSb9hz7uhpNfYvkCgFRbJaFgSxmaBBi1eqw0= X-Google-Smtp-Source: ABdhPJx9GkuiGQMtkuSZAayYU//ImBOy7fJdgDiZXBxHb2eTmcTTHWVrPwwVuybIvcmrB3WJ0sNw8uCPklbfHGEk3vE= X-Received: by 2002:adf:a306:: with SMTP id c6mr5496064wrb.160.1602682138603; Wed, 14 Oct 2020 06:28:58 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:adf:c448:0:0:0:0:0 with HTTP; Wed, 14 Oct 2020 06:28:57 -0700 (PDT) In-Reply-To: <202010141228.09ECSg0D023438@repo.freebsd.org> References: <202010141228.09ECSg0D023438@repo.freebsd.org> From: Mateusz Guzik Date: Wed, 14 Oct 2020 15:28:57 +0200 Message-ID: Subject: Re: svn commit: r366697 - head/usr.bin/xinstall To: Alex Richardson Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 4CBCtR0JSKz404y X-Spamd-Bar: ---- Authentication-Results: mx1.freebsd.org; none X-Spamd-Result: default: False [-4.00 / 15.00]; ASN(0.00)[asn:15169, ipnet:2a00:1450::/32, country:US]; REPLY(-4.00)[] X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 14 Oct 2020 13:29:03 -0000 This should use copy_file_range (also available on Linux). On 10/14/20, Alex Richardson wrote: > Author: arichardson > Date: Wed Oct 14 12:28:41 2020 > New Revision: 366697 > URL: https://svnweb.freebsd.org/changeset/base/366697 > > Log: > install(1): Avoid unncessary fstatfs() calls and use mmap() based on size > > According to git blame the trymmap() function was added in 1996 to skip > mmap() calls for NFS file systems. However, nowadays mmap() should be > perfectly safe even on NFS. Importantly, onl ufs and cd9660 file systems > were whitelisted so we don't use mmap() on ZFS. It also prevents the use > of mmap() when bootstrapping from macOS/Linux since on those systems the > trymmap() function was always returning zero due to the missing > MFSNAMELEN > define. > > This change keeps the trymmap() function but changes it to check whether > using mmap() can reduce the number of system calls that are required. > Using mmap() only reduces the number of system calls if we need multiple > read() > syscalls, i.e. if the file size is > MAXBSIZE. However, mmap() is more > expensive > than read() so this sets the threshold at 4 fewer syscalls. Additionally, > for > larger file size mmap() can significantly increase the number of page > faults, > so avoid it in that case. > > It's unclear whether using mmap() is ever faster than a read with an > appropriate > buffer size, but this change at least removes two unnecessary system > calls > for every file that is installed. > > Reviewed By: markj > Differential Revision: https://reviews.freebsd.org/D26041 > > Modified: > head/usr.bin/xinstall/xinstall.c > > Modified: head/usr.bin/xinstall/xinstall.c > ============================================================================== > --- head/usr.bin/xinstall/xinstall.c Wed Oct 14 10:12:39 2020 (r366696) > +++ head/usr.bin/xinstall/xinstall.c Wed Oct 14 12:28:41 2020 (r366697) > @@ -148,7 +148,7 @@ static void metadata_log(const char *, const char *, s > const char *, const char *, off_t); > static int parseid(const char *, id_t *); > static int strip(const char *, int, const char *, char **); > -static int trymmap(int); > +static int trymmap(size_t); > static void usage(void); > > int > @@ -1087,7 +1087,7 @@ compare(int from_fd, const char *from_name __unused, > s > if (do_digest) > digest_init(&ctx); > done_compare = 0; > - if (trymmap(from_fd) && trymmap(to_fd)) { > + if (trymmap(from_len) && trymmap(to_len)) { > p = mmap(NULL, from_len, PROT_READ, MAP_SHARED, > from_fd, (off_t)0); > if (p == MAP_FAILED) > @@ -1248,13 +1248,8 @@ copy(int from_fd, const char *from_name, int to_fd, > co > > digest_init(&ctx); > > - /* > - * Mmap and write if less than 8M (the limit is so we don't totally > - * trash memory on big files. This is really a minor hack, but it > - * wins some CPU back. > - */ > done_copy = 0; > - if (size <= 8 * 1048576 && trymmap(from_fd) && > + if (trymmap((size_t)size) && > (p = mmap(NULL, (size_t)size, PROT_READ, MAP_SHARED, > from_fd, (off_t)0)) != MAP_FAILED) { > nw = write(to_fd, p, size); > @@ -1523,20 +1518,23 @@ usage(void) > * return true (1) if mmap should be tried, false (0) if not. > */ > static int > -trymmap(int fd) > +trymmap(size_t filesize) > { > -/* > - * The ifdef is for bootstrapping - f_fstypename doesn't exist in > - * pre-Lite2-merge systems. > - */ > -#ifdef MFSNAMELEN > - struct statfs stfs; > - > - if (fstatfs(fd, &stfs) != 0) > - return (0); > - if (strcmp(stfs.f_fstypename, "ufs") == 0 || > - strcmp(stfs.f_fstypename, "cd9660") == 0) > - return (1); > -#endif > - return (0); > + /* > + * This function existed to skip mmap() for NFS file systems whereas > + * nowadays mmap() should be perfectly safe. Nevertheless, using mmap() > + * only reduces the number of system calls if we need multiple read() > + * syscalls, i.e. if the file size is > MAXBSIZE. However, mmap() is > + * more expensive than read() so set the threshold at 4 fewer syscalls. > + * Additionally, for larger file size mmap() can significantly increase > + * the number of page faults, so avoid it in that case. > + * > + * Note: the 8MB limit is not based on any meaningful benchmarking > + * results, it is simply reusing the same value that was used before > + * and also matches bin/cp. > + * > + * XXX: Maybe we shouldn't bother with mmap() at all, since we use > + * MAXBSIZE the syscall overhead of read() shouldn't be too high? > + */ > + return (filesize > 4 * MAXBSIZE && filesize < 8 * 1024 * 1024); > } > _______________________________________________ > svn-src-all@freebsd.org mailing list > https://lists.freebsd.org/mailman/listinfo/svn-src-all > To unsubscribe, send any mail to "svn-src-all-unsubscribe@freebsd.org" > -- Mateusz Guzik