xref: /spdk/test/ftl/trim.sh (revision 1b566ac7d9e9159257b881d7dc5675cd9b7e1961)
1#!/usr/bin/env bash
2#  SPDX-License-Identifier: BSD-3-Clause
3#  Copyright (C) 2022 Intel Corporation
4#  All rights reserved.
5#
6
7testdir=$(readlink -f $(dirname $0))
8rootdir=$(readlink -f $testdir/../..)
9source $rootdir/test/common/autotest_common.sh
10source $testdir/common.sh
11
12rpc_py=$rootdir/scripts/rpc.py
13
14fio_kill() {
15	rm -f $testdir/testfile.md5
16	rm -f $testdir/config/ftl.json
17	rm -f $testdir/random_pattern
18	rm -f $file
19
20	killprocess $svcpid
21}
22
23device=$1
24cache_device=$2
25timeout=240
26data_size_in_blocks=$((65536))
27unmap_size_in_blocks=$((1024))
28
29if [[ $CONFIG_FIO_PLUGIN != y ]]; then
30	echo "FIO not available"
31	exit 1
32fi
33
34export FTL_BDEV_NAME=ftl0
35export FTL_JSON_CONF=$testdir/config/ftl.json
36
37trap "fio_kill; exit 1" SIGINT SIGTERM EXIT
38
39"$SPDK_BIN_DIR/spdk_tgt" -m 0x7 &
40svcpid=$!
41waitforlisten $svcpid
42
43split_bdev=$(create_base_bdev nvme0 $device $((1024 * 101)))
44nv_cache=$(create_nv_cache_bdev nvc0 $cache_device $split_bdev)
45
46l2p_percentage=60
47l2p_dram_size_mb=$(($(get_bdev_size $split_bdev) * l2p_percentage / 100 / 1024))
48
49$rpc_py -t $timeout bdev_ftl_create -b ftl0 -d $split_bdev -c $nv_cache --core_mask 7 --l2p_dram_limit $l2p_dram_size_mb --overprovisioning 10
50
51waitforbdev ftl0
52
53(
54	echo '{"subsystems": ['
55	$rpc_py save_subsystem_config -n bdev
56	echo ']}'
57) > $FTL_JSON_CONF
58
59bdev_info=$($rpc_py bdev_get_bdevs -b ftl0)
60nb=$(jq ".[] .num_blocks" <<< "$bdev_info")
61$rpc_py bdev_ftl_unload -b ftl0
62
63killprocess $svcpid
64
65# Generate data pattern
66dd if=/dev/urandom bs=4K count=$data_size_in_blocks > $testdir/random_pattern
67
68# Write data pattern
69"$SPDK_BIN_DIR/spdk_dd" --if=$testdir/random_pattern --ob=ftl0 --json=$FTL_JSON_CONF
70
71"$SPDK_BIN_DIR/spdk_tgt" -L ftl_init &
72svcpid=$!
73waitforlisten $svcpid
74
75$rpc_py load_config < $FTL_JSON_CONF
76
77# Unmap first and last 4MiB
78$rpc_py bdev_ftl_unmap -b ftl0 --lba 0 --num_blocks $((unmap_size_in_blocks))
79$rpc_py bdev_ftl_unmap -b ftl0 --lba $((nb - unmap_size_in_blocks)) --num_blocks $((unmap_size_in_blocks))
80
81killprocess $svcpid
82
83# Calculate checksum of the data written
84file=$testdir/data
85"$SPDK_BIN_DIR/spdk_dd" --ib=ftl0 --of=$file --count=$data_size_in_blocks --json=$FTL_JSON_CONF
86cmp --bytes=$((unmap_size_in_blocks * 4096)) $file /dev/zero
87md5sum $file > $testdir/testfile.md5
88
89# Rewrite the first 4MiB
90"$SPDK_BIN_DIR/spdk_dd" --if=$testdir/random_pattern --ob=ftl0 --count=$((unmap_size_in_blocks)) --json=$FTL_JSON_CONF
91
92"$SPDK_BIN_DIR/spdk_tgt" -L ftl_init &
93svcpid=$!
94waitforlisten $svcpid
95
96$rpc_py load_config < $FTL_JSON_CONF
97
98# Unmap first and last 4MiB
99$rpc_py bdev_ftl_unmap -b ftl0 --lba 0 --num_blocks $((unmap_size_in_blocks))
100$rpc_py bdev_ftl_unmap -b ftl0 --lba $((nb - unmap_size_in_blocks)) --num_blocks $((unmap_size_in_blocks))
101
102killprocess $svcpid
103
104# Verify that the checksum matches and the data is consistent
105"$SPDK_BIN_DIR/spdk_dd" --ib=ftl0 --of=$file --count=$data_size_in_blocks --json=$FTL_JSON_CONF
106md5sum -c $testdir/testfile.md5
107
108trap - SIGINT SIGTERM EXIT
109fio_kill
110