#!/usr/bin/env expect
############################################################################
# Purpose: Stress test with maximum slurmctld message concurrency.
#          We start a large number of job steps, the simultaneously
#          cancel them all. Message can get lost/retransmitted, so
#          there is a delayed test for job step removal.
############################################################################
# Copyright (C) 2002-2006 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# 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_in      "$test_dir/input"
set job_cnt      10
set delay        10
set job_name     $test_name
set sleep_time   300
set task_cnt     60
set job_list    [list]

proc cleanup {} {
	global job_list

	cancel_job $job_list
}

if {![param_contains [get_config_param "SelectTypeParameters"] "CR_*MEMORY"]} {
	set mem_per_step 0
} else {
	set mem_per_step 8
}

#
# A single slurmd can't handle a large task count without
# running out of memory and pthreads
#
if {[get_config_param "FrontendName"] ne "MISSING" || $enable_memory_leak_debug != 0} {
	set task_cnt 2
}

#
# Build input script file
# NOTE: The initial sleep is so that all of the submissions have time
#   to occur before contending with a multitude of job step creations.
#   This is especially important on very slow systems (e.g. AIX).
# NOTE: Explicitly set a small memory limit. Without explicitly setting the step
#   memory limit, it will use the system default (same as the job) which may
#   prevent the level of parallelism desired.
#
make_bash_script $file_in "
$bin_sleep 5
ulimit -u `ulimit -u -H`
for ((inx=0; inx < $task_cnt; inx++)) ; do
$srun -N1 -n1 --mem=0 --overlap $bin_sleep $sleep_time &
done
$srun -N1 -n1 --mem=0 --overlap $bin_sleep $sleep_time
"

#
# Initiate $job_cnt batch jobs
#
set timeout 30
for {set inx 0} {$inx < $job_cnt} {incr inx} {
	set job_id [submit_job -fail "--gres=craynetwork:0 --job-name=$job_name --mem-per-cpu=[expr $mem_per_step * 10] --output=/dev/null --error=/dev/null -t5 $file_in"]
	lappend job_list $job_id
}
log_debug "All $job_cnt jobs submitted"

# Wait for at least $job_cnt jobs to be started
# Because we want an external variable set with the match count, it is
# simpler to use wait_for here than wait_for_command
set job_count 0
if [
	wait_for -timeout $delay {$job_count >= $job_cnt} {
		set job_count [
			regexp -all $job_name [
				run_command_output -fail "$squeue --state R --name $job_name"
			]
		]
	}
] {
	log_warn "Not all jobs were started ($job_count < $job_cnt). This is ok as long as it is at least 1"
}
if {$job_count < 1} {
	fail "No jobs were started"
}

#
# There could be hundreds of job steps, we don't want to see
# the details, but want to make sure that we did start many
#
set desired_tasks [expr $task_cnt * 2 / 3]

# We want to see a decent number of steps running
if [
	wait_for {$step_count >= $desired_tasks} {
		set step_count [
			regexp -all sleep [
				run_command_output -fail "$squeue --steps --name $job_name"
			]
		]
	}
] {
	fail "Only started $job_count jobs and $step_count steps. We expected at least $desired_tasks and possibly hundreds"
}

log_debug "We found $job_count jobs and $step_count steps"
