#! /bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (c) 2024 Luis Chamberlain.  All Rights Reserved.
#
# FS QA Test 750
#
# fsstress + memory compaction test
#
. ./common/preamble
_begin_fstest auto rw long_rw stress soak smoketest

_cleanup()
{
	_kill_fsstress
	rm -f $runfile
	kill -9 $trigger_compaction_pid > /dev/null 2>&1
	wait > /dev/null 2>&1
	rm -f $tmp.*
	cd /
}

# Import common functions.



_require_scratch
_require_vm_compaction

_fixed_by_git_commit kernel d99e3140a4d3 \
	"mm: turn folio_test_hugetlb into a PageType"

#merged on v6.11-rc4
_fixed_by_git_commit kernel 2e6506e1c4ee \
    "mm/migrate: fix deadlock in migrate_pages_batch() on large folios"

echo "Silence is golden"

_scratch_mkfs > $seqres.full 2>&1
_scratch_mount >> $seqres.full 2>&1

nr_cpus=$((LOAD_FACTOR * 4))
nr_ops=$((25000 * TIME_FACTOR))
fsstress_args=(-w -d $SCRATCH_MNT -n $nr_ops -p $nr_cpus)
test -n "$SOAK_DURATION" && fsstress_args+=(--duration="$SOAK_DURATION")

# start a background trigger for memory compaction
runfile="$tmp.compaction"
touch $runfile
while [ -e $runfile ]; do
	echo 1 > /proc/sys/vm/compact_memory
	sleep 5
done &
trigger_compaction_pid=$!

_run_fsstress "${fsstress_args[@]}"

rm -f $runfile
wait > /dev/null 2>&1

status=0
exit
