1#!/bin/ksh -p
2#
3# CDDL HEADER START
4#
5# This file and its contents are supplied under the terms of the
6# Common Development and Distribution License ("CDDL"), version 1.0.
7# You may only use this file in accordance with the terms of version
8# 1.0 of the CDDL.
9#
10# A full copy of the text of the CDDL should have accompanied this
11# source.  A copy of the CDDL is also available via the Internet at
12# http://www.illumos.org/license/CDDL.
13#
14# CDDL HEADER END
15#
16
17#
18# Copyright (c) 2017 by Lawrence Livermore National Security, LLC.
19#
20
21# DESCRIPTION:
22#	Verify MMP writes are distributed evenly among leaves
23#
24# STRATEGY:
25#	1. Create an asymmetric mirrored pool
26#	2. Enable multihost and multihost_history
27#	3. Delay for MMP writes to occur
28#	4. Verify the MMP writes are distributed evenly across leaf vdevs
29#
30
31. $STF_SUITE/include/libtest.shlib
32. $STF_SUITE/tests/functional/mmp/mmp.cfg
33. $STF_SUITE/tests/functional/mmp/mmp.kshlib
34
35verify_runnable "both"
36
37function cleanup
38{
39	log_must zpool destroy $MMP_POOL
40	log_must rm $MMP_DIR/file.{0,1,2,3,4,5,6,7}
41	log_must rm $MMP_HISTORY_TMP
42	log_must rmdir $MMP_DIR
43	log_must mmp_clear_hostid
44}
45
46log_assert "mmp writes are evenly distributed across leaf vdevs"
47log_onexit cleanup
48
49MMP_HISTORY_TMP=$MMP_DIR/history
50MMP_HISTORY=/proc/spl/kstat/zfs/$MMP_POOL/multihost
51
52# Step 1
53log_must mkdir -p $MMP_DIR
54log_must truncate -s 128M $MMP_DIR/file.{0,1,2,3,4,5,6,7}
55log_must zpool create -f $MMP_POOL mirror $MMP_DIR/file.{0,1} mirror $MMP_DIR/file.{2,3,4,5,6,7}
56
57# Step 2
58log_must mmp_set_hostid $HOSTID1
59log_must zpool set multihost=on $MMP_POOL
60set_tunable64 zfs_multihost_history 0
61set_tunable64 zfs_multihost_history 40
62
63# Step 3
64# default settings, every leaf written once/second
65sleep 4
66
67# Step 4
68typeset -i min_writes=999
69typeset -i max_writes=0
70typeset -i write_count
71# copy to get as close to a consistent view as possible
72cat $MMP_HISTORY > $MMP_HISTORY_TMP
73for x in $(seq 0 7); do
74	write_count=$(grep -c file.${x} $MMP_HISTORY_TMP)
75	if [ $write_count -lt $min_writes ]; then
76		min_writes=$write_count
77	fi
78	if [ $write_count -gt $max_writes ]; then
79		max_writes=$write_count
80	fi
81done
82log_note "mmp min_writes $min_writes max_writes $max_writes"
83
84if [ $min_writes -lt 1 ]; then
85	log_fail "mmp writes were not counted correctly"
86fi
87
88if [ $((max_writes - min_writes)) -gt 1 ]; then
89	log_fail "mmp writes were not evenly distributed across leaf vdevs"
90fi
91
92log_pass "mmp writes were evenly distributed across leaf vdevs"
93