#!/bin/sh -e

##########################################################################
#   Function description:
#       Pause until user presses return
##########################################################################

pause()
{
    local junk
    
    printf "Press return to continue..."
    read junk
}

##########################################################################
#   Description:
#       Set address space limit limits.d
#       FIXME: This is a temporary hack used for SPCM quick setup.
#       A more robust and flexible design would be good.
#
#   Arguments:
#       Virtual memory limit in kilobytes
#
#   History:
#   Date        Name        Modification
#   2013-12-14  root        Begin
##########################################################################
    
case $(auto-ostype) in
RHEL)
    
    case $(auto-os-release) in
    RHEL6)
	;;
    
    RHEL7)
	;;

    *)
	auto-unsupported-release $0
	exit 1
	;;

    esac
    
    if [ $# != 1 ]; then
	printf "Usage: $0 kilobytes\n"
	exit 1
    fi
    
    mem_limit=$1
    
    # On controller nodes, set limits fairly high due to recent Linux kernel's
    # method of measuring memory for multithreaded processes.
    #*   soft    as      16777216
    #*   hard    as      16777216
    #*   soft    memlock 16777216
    #*   hard    memlock 16777216
    
    # Note: datasize is also controlled by --mem-per-cpu, so the limits
    # below may not apply.
    
    # Setting stack to all available RAM causes failure in thread startup.
    # Setting to unlimited instead allows it to work.
    # Limit only as (vmemory) and set the rest to unlimited.
    cat << EOM > /etc/security/limits.d/91-memory.conf
*   soft    as      $mem_limit
*   hard    as      $mem_limit
*   soft    data    unlimited
*   hard    data    unlimited
*   soft    memlock unlimited
*   hard    memlock unlimited
*   soft    stack   unlimited
*   hard    stack   unlimited
EOM
    
    cat << EOM > /etc/sysconfig/slurm
ulimit -v $mem_limit
ulimit -d unlimited
ulimit -l unlimited
ulimit -s unlimited

# Memlocks the slurmd process's memory so that when a node starts swapping,
# the slurmd will continue to respond
SLURMD_OPTIONS="-M"
EOM
    
    chmod 644 /etc/security/limits.d/91-memory.conf /etc/sysconfig/slurm
    ;;

FreeBSD)
    : ${EDITOR:=vi}
    realmem=$(sysctl -n hw.realmem)
    echo $realmem
    maxdsiz=$(($realmem * 2))
    echo $maxdsiz
    auto-set-conf-var kern.maxdsiz $maxdsiz /boot/loader.conf $0
    cat << EOM

Editing /etc/login.conf.  Change vmemoryuse under the appropriate user class.
New limits will take effect upon the next login.

EOM
    pause
    $EDITOR /etc/login.conf
    cap_mkdb /etc/login.conf
    ;;

*)
    auto-unsupported-os $0
    exit 1
    ;;

esac
