#!/bin/sh
# Copyright (c) 2016 Oracle and/or its affiliates. All Rights Reserved.
# Copyright (c) International Business Machines  Corp., 2001
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it would be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
#

TCID=nfsstat01
TST_TOTAL=4
TST_CLEANUP="nfs_cleanup"

. nfs_lib.sh
TST_USE_LEGACY_API=1
. tst_net.sh

setup()
{
	tst_check_cmds nfsstat
	nfs_setup
}

get_calls()
{
	local name=$1
	local field=$2
	local nfs_f=$3
	local calls=
	local opt=
	[ "$name" = "rpc" ] && opt="r" || opt="n"

	if [ "$nfs_f" = "nfs" ]; then
		calls="$(grep $name /proc/net/rpc/$nfs_f | cut -d' ' -f$field)"
		ROD nfsstat -c$opt | grep -q "$calls"
		echo "$calls"
		return
	fi

	calls=$(tst_rhost_run -c "grep $name /proc/net/rpc/$nfs_f" | \
		cut -d' ' -f$field)
	tst_rhost_run -s -c "nfsstat -s$opt" | grep -q "$calls"
	echo "$calls"
}

# PURPOSE:  Performs simple copies and removes to verify statistic
#           tracking using the 'nfsstat' command and /proc/net/rpc
do_test()
{
	tst_resm TINFO "checking RPC calls for server/client"

	local server_calls="$(get_calls rpc 2 nfsd)"
	local client_calls="$(get_calls rpc 2 nfs)"

	tst_resm TINFO "calls $server_calls/$client_calls"

	tst_resm TINFO "Checking for tracking of RPC calls for server/client"
	cat /proc/cpuinfo > nfsstat01.tmp

	local new_server_calls="$(get_calls rpc 2 nfsd)"
	local new_client_calls="$(get_calls rpc 2 nfs)"
	tst_resm TINFO "new calls $new_server_calls/$new_client_calls"

	if [ "$new_server_calls" -le "$server_calls" ]; then
		tst_resm TFAIL "server RPC calls not increased"
	else
		tst_resm TPASS "server RPC calls increased"
	fi

	if [ "$new_client_calls" -le "$client_calls" ]; then
		tst_resm TFAIL "client RPC calls not increased"
	else
		tst_resm TPASS "client RPC calls increased"
	fi

	tst_resm TINFO "checking NFS calls for server/client"
	local field=
	case $VERSION in
	2) field=13
	;;
	*) field=15
	;;
	esac

	server_calls="$(get_calls proc$VERSION $field nfsd)"
	client_calls="$(get_calls proc$VERSION $field nfs)"
	tst_resm TINFO "calls $server_calls/$client_calls"

	tst_resm TINFO "Checking for tracking of NFS calls for server/client"
	rm -f nfsstat01.tmp

	new_server_calls="$(get_calls proc$VERSION $field nfsd)"
	new_client_calls="$(get_calls proc$VERSION $field nfs)"
	tst_resm TINFO "new calls $new_server_calls/$new_client_calls"

	if [ "$new_server_calls" -le "$server_calls" ]; then
		tst_resm TFAIL "server NFS calls not increased"
	else
		tst_resm TPASS "server NFS calls increased"
	fi

	if [ "$new_client_calls" -le "$client_calls" ]; then
		tst_resm TFAIL "client NFS calls not increased"
	else
		tst_resm TPASS "client NFS calls increased"
	fi
}

setup

do_test

tst_exit
