Skip to content

Commit 97fe868

Browse files
robnbehlendorf
authored andcommitted
ZTS: mmap_ftruncate test to confirm async writeback behaviour
Sponsored-by: Klara, Inc. Sponsored-by: Wasabi Technology, Inc. Reviewed-by: Brian Behlendorf <[email protected]> Reviewed-by: Alexander Motin <[email protected]> Signed-off-by: Rob Norris <[email protected]> Closes #17584
1 parent df5e02d commit 97fe868

File tree

7 files changed

+172
-2
lines changed

7 files changed

+172
-2
lines changed

tests/runfiles/common.run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ tags = ['functional', 'migration']
788788

789789
[tests/functional/mmap]
790790
tests = ['mmap_mixed', 'mmap_read_001_pos', 'mmap_seek_001_pos',
791-
'mmap_sync_001_pos', 'mmap_write_001_pos']
791+
'mmap_sync_001_pos', 'mmap_write_001_pos', 'mmap_ftruncate']
792792
tags = ['functional', 'mmap']
793793

794794
[tests/functional/mount]

tests/zfs-tests/cmd/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
/mkfiles
2424
/mktree
2525
/mmap_exec
26+
/mmap_ftruncate
2627
/mmap_libaio
2728
/mmap_seek
2829
/mmap_sync

tests/zfs-tests/cmd/Makefile.am

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,9 @@ scripts_zfs_tests_bin_PROGRAMS += %D%/mkbusy %D%/mkfile %D%/mkfiles %D%/mktree
7272
%C%_mkfile_LDADD = $(LTLIBINTL)
7373

7474

75-
scripts_zfs_tests_bin_PROGRAMS += %D%/mmap_exec %D%/mmap_seek %D%/mmap_sync %D%/mmapwrite %D%/readmmap
75+
scripts_zfs_tests_bin_PROGRAMS += \
76+
%D%/mmap_exec %D%/mmap_ftruncate %D%/mmap_seek \
77+
%D%/mmap_sync %D%/mmapwrite %D%/readmmap
7678
%C%_mmapwrite_LDADD = -lpthread
7779

7880
if WANT_MMAP_LIBAIO
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// SPDX-License-Identifier: CDDL-1.0
2+
/*
3+
* CDDL HEADER START
4+
*
5+
* The contents of this file are subject to the terms of the
6+
* Common Development and Distribution License (the "License").
7+
* You may not use this file except in compliance with the License.
8+
*
9+
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10+
* or http://opensource.org/licenses/CDDL-1.0.
11+
* See the License for the specific language governing permissions
12+
* and limitations under the License.
13+
*
14+
* When distributing Covered Code, include this CDDL HEADER in each
15+
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16+
* If applicable, add the following below this CDDL HEADER, with the
17+
* fields enclosed by brackets "[]" replaced with your own identifying
18+
* information: Portions Copyright [yyyy] [name of copyright owner]
19+
*
20+
* CDDL HEADER END
21+
*/
22+
23+
/*
24+
* Copyright (c) 2025, Klara, Inc.
25+
*/
26+
27+
/*
28+
* Tests async writeback behaviour. Creates a file, maps it into memory, and
29+
* dirties every page within it. Then, calls ftruncate() to collapse the file
30+
* back down to 0. This causes the kernel to begin writeback on the dirty
31+
* pages so they can be freed, before it can complete the ftruncate() call.
32+
* None of these are sync operations, so they should avoid the various "force
33+
* flush" codepaths.
34+
*/
35+
36+
#include <unistd.h>
37+
#include <fcntl.h>
38+
#include <sys/stat.h>
39+
#include <sys/mman.h>
40+
#include <stdlib.h>
41+
#include <stdio.h>
42+
43+
#define _pdfail(f, l, s) \
44+
do { perror("[" f "#" #l "] " s); exit(2); } while (0)
45+
#define pdfail(str) _pdfail(__FILE__, __LINE__, str)
46+
47+
int
48+
main(int argc, char **argv) {
49+
if (argc != 3) {
50+
printf("usage: mmap_ftruncate <file> <size>\n");
51+
exit(2);
52+
}
53+
54+
const char *file = argv[1];
55+
56+
char *end;
57+
off_t sz = strtoull(argv[2], &end, 0);
58+
if (end == argv[2] || *end != '\0' || sz == 0) {
59+
fprintf(stderr, "E: invalid size");
60+
exit(2);
61+
}
62+
63+
int fd = open(file, O_CREAT|O_TRUNC|O_RDWR, S_IRUSR|S_IWUSR);
64+
if (fd < 0)
65+
pdfail("open");
66+
67+
if (ftruncate(fd, sz) < 0)
68+
pdfail("ftruncate");
69+
70+
char *p = mmap(NULL, sz, PROT_WRITE, MAP_SHARED, fd, 0);
71+
if (p == MAP_FAILED)
72+
pdfail("mmap");
73+
74+
for (off_t off = 0; off < sz; off += 4096)
75+
p[off] = 1;
76+
77+
if (ftruncate(fd, 0) < 0)
78+
pdfail("ftruncate");
79+
80+
if (munmap(p, sz) < 0)
81+
pdfail("munmap");
82+
83+
close(fd);
84+
return (0);
85+
}

tests/zfs-tests/include/commands.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export ZFSTEST_FILES='badsend
205205
mkfiles
206206
mktree
207207
mmap_exec
208+
mmap_ftruncate
208209
mmap_libaio
209210
mmap_seek
210211
mmap_sync

tests/zfs-tests/tests/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1660,6 +1660,7 @@ nobase_dist_datadir_zfs_tests_tests_SCRIPTS += \
16601660
functional/mmap/mmap_seek_001_pos.ksh \
16611661
functional/mmap/mmap_sync_001_pos.ksh \
16621662
functional/mmap/mmap_write_001_pos.ksh \
1663+
functional/mmap/mmap_ftruncate.ksh \
16631664
functional/mmap/setup.ksh \
16641665
functional/mmp/cleanup.ksh \
16651666
functional/mmp/mmp_active_import.ksh \
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/bin/ksh -p
2+
# SPDX-License-Identifier: CDDL-1.0
3+
#
4+
# CDDL HEADER START
5+
#
6+
# The contents of this file are subject to the terms of the
7+
# Common Development and Distribution License (the "License").
8+
# You may not use this file except in compliance with the License.
9+
#
10+
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11+
# or https://opensource.org/licenses/CDDL-1.0.
12+
# See the License for the specific language governing permissions
13+
# and limitations under the License.
14+
#
15+
# When distributing Covered Code, include this CDDL HEADER in each
16+
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17+
# If applicable, add the following below this CDDL HEADER, with the
18+
# fields enclosed by brackets "[]" replaced with your own identifying
19+
# information: Portions Copyright [yyyy] [name of copyright owner]
20+
#
21+
# CDDL HEADER END
22+
#
23+
24+
#
25+
# Copyright (c) 2025, Klara, Inc.
26+
#
27+
28+
. $STF_SUITE/include/libtest.shlib
29+
30+
#
31+
# This verifies that async writeback of dirty mmap()'d pages completes quickly.
32+
# ftruncate() is an operation that will trigger async writeback, but is not
33+
# itself a syncing operation, making it a useful proxy for any way the kernel
34+
# might trigger async writeback.
35+
#
36+
# The guts of this test is in the mmap_ftruncate program. This driver sets a
37+
# larger zfs_txg_timeout. Test failure occurs ftruncate() blocks waiting for
38+
# the writeback until the txg timeout is reached and the changes are forcibly
39+
# written out. Success means the DMU has accepted the changes and cleared the
40+
# page dirty flags.
41+
#
42+
43+
TIMEOUT=180
44+
TESTFILE=/$TESTPOOL/truncfile
45+
TESTSIZE=$((2*1024*1024*1024)) # 2G
46+
47+
verify_runnable "global"
48+
49+
typeset claim="async writeback of dirty mmap()'d pages completes quickly"
50+
51+
log_assert $claim
52+
53+
log_must save_tunable TXG_TIMEOUT
54+
55+
function cleanup
56+
{
57+
log_must restore_tunable TXG_TIMEOUT
58+
rm -f $TESTFILE
59+
}
60+
log_onexit cleanup
61+
62+
log_must set_tunable32 TXG_TIMEOUT $TIMEOUT
63+
log_must zpool sync -f
64+
65+
# run mmap_ftruncate and record the run time
66+
typeset -i start=$(date +%s)
67+
log_must mmap_ftruncate $TESTFILE $TESTSIZE
68+
typeset -i end=$(date +%s)
69+
typeset -i delta=$((end - start))
70+
71+
# in practice, mmap_ftruncate needs a few seconds to dirty all the pages, and
72+
# when this test passes, the ftruncate() call itself should be near-instant.
73+
# when it fails, then its only the txg sync that allows ftruncate() to
74+
# complete, in that case, the run time will be extremely close to the timeout,
75+
# so to avoid any confusion at the edges, we require that it complets within
76+
# half the transaction time. for any timeout higher than ~30s that should be a
77+
# very bright line down the middle.
78+
log_must test $delta -lt $((TIMEOUT / 2))
79+
80+
log_pass $claim

0 commit comments

Comments
 (0)