Add Linux posix_fadvise support

The purpose of this PR is to accepts fadvise ioctl from userland
to do read-ahead by demand.

It could dramatically improve sequential read performance especially
when primarycache is set to metadata or zfs_prefetch_disable is 1.

If the file is mmaped, generic_fadvise is also called for page cache
read-ahead besides dmu_prefetch.

Only POSIX_FADV_WILLNEED and POSIX_FADV_SEQUENTIAL are supported in
this PR currently.

Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Finix Yan <yancw@info2soft.com>
Closes #13694
This commit is contained in:
Finix1979
2022-09-09 01:29:41 +08:00
committed by GitHub
parent 380b08098e
commit 320f0c6022
15 changed files with 365 additions and 2 deletions
+4
View File
@@ -89,6 +89,10 @@ tags = ['functional', 'devices']
tests = ['events_001_pos', 'events_002_pos', 'zed_rc_filter', 'zed_fd_spill']
tags = ['functional', 'events']
[tests/functional/fadvise:Linux]
tests = ['fadvise_sequential']
tags = ['functional', 'fadvise']
[tests/functional/fallocate:Linux]
tests = ['fallocate_prealloc', 'fallocate_zero-range']
tags = ['functional', 'fallocate']
+1
View File
@@ -4,6 +4,7 @@
/devname2devid
/dir_rd_update
/draid
/file_fadvise
/file_append
/file_check
/file_trunc
+3
View File
@@ -128,4 +128,7 @@ scripts_zfs_tests_bin_PROGRAMS += %D%/read_dos_attributes %D%/write_dos_attribu
scripts_zfs_tests_bin_PROGRAMS += %D%/randfree_file
%C%_randfree_file_SOURCES = %D%/file/randfree_file.c
scripts_zfs_tests_bin_PROGRAMS += %D%/file_fadvise
%C%_file_fadvise_SOURCES = %D%/file/file_fadvise.c
endif
+97
View File
@@ -0,0 +1,97 @@
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or https://opensource.org/licenses/CDDL-1.0.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2007 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2022 by Information2 Software, Inc. All rights reserved.
*/
#include "file_common.h"
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
/*
* Call fadvise to prefetch data
*/
static const char *execname = "file_fadvise";
static void
usage(void)
{
(void) fprintf(stderr,
"usage: %s -f filename -a advise \n", execname);
}
int
main(int argc, char *argv[])
{
char *filename = NULL;
int advise = 0;
int fd, ch;
int err = 0;
while ((ch = getopt(argc, argv, "a:f:")) != EOF) {
switch (ch) {
case 'a':
advise = atoll(optarg);
break;
case 'f':
filename = optarg;
break;
case '?':
(void) printf("unknown arg %c\n", optopt);
usage();
break;
}
}
if (!filename) {
(void) printf("Filename not specified (-f <file>)\n");
err++;
}
if (advise < POSIX_FADV_NORMAL || advise > POSIX_FADV_NOREUSE) {
(void) printf("advise is invalid\n");
err++;
}
if (err) {
usage(); /* no return */
return (1);
}
if ((fd = open(filename, O_RDWR, 0666)) < 0) {
perror("open");
return (1);
}
posix_fadvise(fd, 0, 0, advise);
close(fd);
return (0);
}
+1
View File
@@ -184,6 +184,7 @@ export ZFSTEST_FILES='badsend
devname2devid
dir_rd_update
draid
file_fadvise
file_append
file_check
file_trunc
+3
View File
@@ -1370,6 +1370,9 @@ nobase_dist_datadir_zfs_tests_tests_SCRIPTS += \
functional/exec/exec_001_pos.ksh \
functional/exec/exec_002_neg.ksh \
functional/exec/setup.ksh \
functional/fadvise/cleanup.ksh \
functional/fadvise/fadvise_sequential.ksh \
functional/fadvise/setup.ksh \
functional/fallocate/cleanup.ksh \
functional/fallocate/fallocate_prealloc.ksh \
functional/fallocate/fallocate_punch-hole.ksh \
@@ -76,7 +76,7 @@ while [[ $j -lt ${#CHECKSUM_TYPES[*]} ]]; do
log_must zpool export $TESTPOOL
log_must zpool import $TESTPOOL
log_mustnot eval "cat $TESTDIR/test_$type >/dev/null"
log_mustnot eval "dd if=$TESTDIR/test_$type of=/dev/null bs=$WRITESZ count=$NWRITES"
cksum=$(zpool status -P -v $TESTPOOL | grep "$firstvdev" | \
awk '{print $5}')
+28
View File
@@ -0,0 +1,28 @@
#!/bin/ksh -p
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or https://opensource.org/licenses/CDDL-1.0.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Portions Copyright (c) 2022 Information2 Software, Inc.
#
. $STF_SUITE/include/libtest.shlib
default_cleanup
@@ -0,0 +1,80 @@
#!/bin/ksh -p
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or https://opensource.org/licenses/CDDL-1.0.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
#
# Portions Copyright (c) 2022 Information2 Software, Inc.
#
. $STF_SUITE/include/libtest.shlib
. $STF_SUITE/include/math.shlib
#
# DESCRIPTION:
# Test posix_fadvise.
#
# STRATEGY:
# 1. Set primarycache to metadata in order to disable prefetch
# 2. Write some data to file
# 3. get data_size field from arcstat
# 4. call file_fadvise with POSIX_FADV_SEQUENTIAL
# 5. get data_size field from arcstat again
# 6. latter data_size should be bigger than former one
#
# NOTE: if HAVE_FILE_FADVISE is not defined former data_size
# should less or eaqul to latter one
verify_runnable "global"
FILE=$TESTDIR/$TESTFILE0
BLKSZ=$(get_prop recordsize $TESTPOOL)
function cleanup
{
log_must zfs set primarycache=all $TESTPOOL
[[ -e $TESTDIR ]] && log_must rm -Rf $TESTDIR/*
}
getstat() {
awk -v c="$1" '$1 == c {print $3; exit}' /proc/spl/kstat/zfs/arcstats
}
log_assert "Ensure fadvise prefetch data"
log_onexit cleanup
log_must zfs set primarycache=metadata $TESTPOOL
log_must file_write -o create -f $FILE -b $BLKSZ -c 1000
sync_pool $TESTPOOL
data_size1=$(getstat data_size)
log_must file_fadvise -f $FILE -a 2
sleep 10
data_size2=$(getstat data_size)
log_note "original data_size is $data_size1, final data_size is $data_size2"
log_must [ $data_size1 -le $data_size2 ]
log_pass "Ensure data could be prefetched"
+30
View File
@@ -0,0 +1,30 @@
#!/bin/ksh -p
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or https://opensource.org/licenses/CDDL-1.0.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Portions Copyright (c) 2022 Information2 Software, Inc.
#
. $STF_SUITE/include/libtest.shlib
DISK=${DISKS%% *}
default_setup_noexit $DISK
log_pass
@@ -73,7 +73,7 @@ for type in "mirror" "raidz" "raidz2"; do
# 4. Inject CHECKSUM ERRORS on read with a zinject error handler
log_must zinject -d $FAULT_FILE -e corrupt -f 50 -T read $TESTPOOL
log_must cp $TESTFILE /dev/null
log_must dd if=$TESTFILE of=/dev/null bs=1M count=64
# 5. Verify the ZED kicks in a hot spare and expected pool/device status
log_note "Wait for ZED to auto-spare"