#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Decrease size of job with allocated MPS
############################################################################
# Copyright (C) SchedMD LLC.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm 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.
#
# Slurm is distributed in the hope that it will 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 Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set file_in1         "$test_dir/input1"
set file_in2         "$test_dir/input2"
set file_out         "$test_dir/output"
set job_id           0

if {![check_config_select "cons_tres"]} {
    skip "This test is only compatible with select/cons_tres"
}
if {[get_config_param "FrontendName"] ne "MISSING"} {
    skip "This test is incompatible with front-end systems"
}

set constrain_devices [expr {[get_config_param "ConstrainDevices"] eq "yes"}]
if {$constrain_devices} {
	log_debug "Devices files are constrained by cgroups"
} else {
	log_debug "Devices files are NOT constrained by cgroups"
}

if {[llength [get_nodes_by_request "--gres=mps:100 -N2 -t2"]] == 0} {
	skip "This test requires being able to submit job with --gres=mps:100 -N2"
}

proc cleanup {} {
	global job_id

	cancel_job $job_id
}

#
# Build input scripts
# file_in1: Determine MPS allocated, shrink and test again
# file_in2: Print the hostname and MPS info
#
make_bash_script $file_in1 "
	$scontrol -dd show job \${SLURM_JOBID}
	$srun $file_in2
	$scontrol update JobId=\${SLURM_JOBID} NumNodes=1
	. slurm_job_\${SLURM_JOBID}_resize.sh
	$scontrol -dd show job \${SLURM_JOBID}
	$srun $file_in2
	$bin_rm -f slurm_job_\${SLURM_JOBID}_resize.csh
	$bin_rm -f slurm_job_\${SLURM_JOBID}_resize.sh
	exit 0"

# NOTE: We pipe stderr from slurmd and discard stdout
make_bash_script $file_in2 "
$slurmd -N \$SLURMD_NODENAME -G 2>&1 >/dev/null | grep 'Gres Name=mps' | grep 'Index='\$CUDA_VISIBLE_DEVICES
echo 'HOST:'\$SLURMD_NODENAME 'CUDA_VISIBLE_DEVICES:'\$CUDA_VISIBLE_DEVICES 'CUDA_MPS_ACTIVE_THREAD_PERCENTAGE:'\$CUDA_MPS_ACTIVE_THREAD_PERCENTAGE"

#
# Submit job to shrink: uses 10 gres/mps on two nodes
#
set job_id [submit_job -fail "-J $test_name -t2 --gres=mps:10 -N2 --output=$file_out $file_in1"]
wait_for_job -fail $job_id "DONE"

#
# Parse the output files from job 1
#
log_info "Parse job 1 output"
wait_for_file -fail $file_out
set match 0
set percentage -1
spawn $bin_cat $file_out
expect {
	-re "CUDA_VISIBLE_DEVICES:($number) CUDA_MPS_ACTIVE_THREAD_PERCENTAGE:($number)" {
		incr match
		set percentage $expect_out(2,string)
		exp_continue
	}
	eof {
		wait
	}
}
if {$match != 3} {
	fail "Bad CUDA information about job 1 ($match != 3)"
}

#
# If devices not constrained and Count on all allocated devices is the same
# then confirm the CUDA_MPS_ACTIVE_THREAD_PERCENTAGE value above is correct
#
if {$constrain_devices == 0} {
	set count -1
	log_user 0
	spawn $bin_cat $file_out
	expect {
		-re "Count=($number)" {
			if {$count == -1} {
				set count $expect_out(1,string)
			} elseif {$count != $expect_out(1,string)} {
				set count -1
			}
			exp_continue
		}
		eof {
			wait
		}
	}
	log_user 1
	if {$count > 0} {
		set count [expr 10 * 100 / $count]
		if {$percentage != $count} {
			fail "Bad CUDA_MPS_ACTIVE_THREAD_PERCENTAGE value ($percentage != $count)"
		} else {
			log_debug "CUDA_MPS_ACTIVE_THREAD_PERCENTAGE value is good"
		}
	}
}
