#!/usr/bin/env expect
############################################################################
# Purpose: Test of SLURM functionality
#          Test of cpus-per-task option on a single node (--cpus-per-task
#          option).
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "WARNING: ..." with an explanation of why the test can't be made, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
#
# NOTE:    This assumes node names are of the form <alpha><number>, where
#          the value of <number> indicates the nodes relative location.
#          Change tha node name parsing logic as needed for other formats.
############################################################################
# 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 test_id     "1.84"
set exit_code   0
set file_in     "test$test_id.bash"

print_header $test_id

if { [test_front_end] } {
	send_user "\nWARNING: This test is incompatible with FRONT_END systems\n"
	exit 0
}

# Various configurations allocate nodes, sockets, cores, cpus or threads;
# not all of which are compatible with this test
set select_type [test_select_type]
if {[string compare $select_type "cons_res"]} {
	send_user "\nWARNING: This test is incompatible with select/$select_type\n"
	exit 0
}

if {[test_launch_poe]} {
	set taskid_env MP_CHILD
} else {
	set taskid_env SLURM_PROCID
}

#
# Submit a 1 node job to determine the node's CPU count
#
set core_spec_cnt  0
set cpu_cnt        0
set cpus_per_node  0
set host           ""
set timeout        $max_job_delay
set task_cnt       0
set threads_per_core 1
set units ""

# find me an idle node in default partition
log_user 0

# find the default partition
set def_part [default_partition]

# find the nodes in the default partition
log_user 0
set def_hostlist ""
set part_exclusive 0
set part_oversubscribe_force 0
spawn $scontrol show part $def_part
expect {
	-re " Nodes=($alpha_numeric_nodelist)"  {
		set def_hostlist $expect_out(1,string)
		exp_continue
	}
	-re " BasePartitions=($alpha_numeric_nodelist)" {
		set def_hostlist $expect_out(1,string)
		exp_continue
	}
	-re " OverSubscribe=EXCLUSIVE" {
		set part_exclusive 1
		exp_continue
	}
	-re " OverSubscribe=FORCE" {
		set part_oversubscribe_force 1
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: scontrol not responding\n"
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$part_oversubscribe_force != 0} {
	send_user "\nWARNING: This test is incompatible with forced sharing of nodes\n"
	exit 0
}

# Add sleep for any epilog clean up of previous jobs
sleep 2
set fd [open "|$scontrol --oneliner show node $def_hostlist"]
exp_internal 1
while {[gets $fd line] != -1} {
	if {[regexp {NodeName=([^\s]+).*CPUTot=(\d+)(K?).*CoreSpecCount=(\d+).*State=IDLE ThreadsPerCore=(\d+)} $line frag host cpu_cnt units core_spec_cnt threads_per_core] == 1} {
		break
	}
	if {[regexp {NodeName=([^\s]+).*CPUTot=(\d+)(K?).*State=IDLE ThreadsPerCore=(\d+)} $line frag host cpu_cnt units threads_per_core] == 1} {
		break
	}
}
exp_internal 0
if {[string compare $units ""]} {
	set cpu_cnt [expr $cpu_cnt * 1024]
}
catch {close $fd}

log_user 1

if {!$cpu_cnt} {
	send_user "\nFAILURE: couldn't find an idle node in the default partition\n"
	exit 1
}

#
# Check CPU count
#
if {[string compare $host ""] == 0} {
	send_user "\nFAILURE: Did not get hostname from idle node\n"
	exit 1
}

send_user "found idle node $host with $cpu_cnt processors\n"

set srun_pid [spawn $srun -N1 -w$host --cpus-per-task=1 --exclusive -l -t1 $bin_printenv $taskid_env]
expect {
	-re "($number):" {
		incr task_cnt
		exp_continue
	}
	-re "Task count specification invalid" {
		send_user "\nWARNING: nodes have too many CPUs for test\n"
		exit 0
 		exp_continue
 	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

# Convert CPU count to core count if necessary
if {$core_spec_cnt != 0} {
	set cpu_cnt [expr $cpu_cnt - ($core_spec_cnt * $threads_per_core)]
}
if {$cpu_cnt != $task_cnt} {
	set core_cnt  $cpu_cnt
	if {$threads_per_core != 0} {
		set cpu_cnt [expr $cpu_cnt * $threads_per_core]
	}
	if {$cpu_cnt == $task_cnt} {
		send_user "NOTE: Allocated $core_cnt cores and $threads_per_core CPUs per core\n"
	}
}
if {$cpu_cnt != $task_cnt} {
	send_user "FAILURE: Should have run $cpu_cnt tasks (one per core) "
	send_user "instead of $task_cnt tasks\n"
	send_user "NOTE: This could be due to memory limit per allocated CPU, MaxCPUsPerNode, CR_ONE_TASK_PER_CORE, or OverSubscribe=FORCE\n\n"
	set exit_code 1
}
if {$cpu_cnt < 2} {
	send_user "\nWARNING: The node only has one core\n"
	exit $exit_code
}

#
# Now verify the --cpus-per-task option
#
set task_cnt  0
set srun_pid [spawn $srun -N1 --cpus-per-task=2 --exclusive -l -t1 --nodelist=$host $bin_printenv $taskid_env]
expect {
	-re "Invalid node name specified" {
		send_user "\nWARNING: Appears you are using "
		send_user "multiple slurmd testing.\n"
		send_user "This test won't work in that fashion.\n"
		exit 0
	}
	-re "$number: *($alpha_numeric_under)" {
		incr task_cnt
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}

#
# Verify that half the number of tasks were spawned
#
if {$task_cnt != [expr $cpu_cnt / 2]} {
	send_user "\nFAILURE: Improper task count for given cpus-per-task\n"
	send_user "NOTE: This could be due to memory limit per allocated CPU or OverSubscribe=FORCE\n\n"
	set exit_code   1
}

if {$exit_code == 0} {
	send_user "\nSUCCESS\n"
}
exit $exit_code
