#!/bin/bash
# Start the backend, with memory limit if possible

. /etc/odemis.conf

RUNNER=""

MAX_FILES=4096 # 1024 is the default, which can be limitting on large microscopes
MEMLIM_FILE=/sys/fs/cgroup/memory/odemisd/memory.limit_in_bytes
MEMSWLIM_FILE=/sys/fs/cgroup/memory/odemisd/memory.memsw.limit_in_bytes

# make sure we don't use too much memory
ulimit -n $MAX_FILES

if [ -d /sys/fs/cgroup/memory/odemisd -a -x /usr/bin/cgexec -a -f "$MEMLIM_FILE" ]; then
    MAXMEM=$(( 3 * 1024 * 1024 * 1024 )) # 3Gb
    # MAXMEMSW is the maximum RAM + swap (must be >= MAXMEM)
    MAXMEMSW=$(( $MAXMEM + 1 * 1024 * 1024 * 1024 )) # + 1Gb
    # ulimit doesn't work because it can only specify virtual memory, which is extremely unpredictable for multiple processes
    #ulimit -S -d $MAXMEM -v $(( 2 * $MAXMEM ))

    if [[ "$(<"$MEMLIM_FILE")" -lt $MAXMEM ]]; then
        # try to do it ourselves
        echo $MAXMEM > "$MEMLIM_FILE"
        MEM_STATUS=$?
    else
        MEM_STATUS=0 #OK
    fi

    # memsw is not supported on all kernels, so if not present, don't worry
    if [[ -f "$MEMSWLIM_FILE" && "$(<$MEMSWLIM_FILE)" -lt $MAXMEMSW ]]; then
        # try to do it ourselves
        echo $MAXMEMSW > "$MEMSWLIM_FILE"
        MEMSW_STATUS=$?
    else
        MEMSW_STATUS=0 #OK
    fi
fi

if [[ $MEM_STATUS -eq 0 && $MEMSW_STATUS -eq 0 ]]; then
    RUNNER="/usr/bin/cgexec -g memory:odemisd"
else
    echo "Warning: failed to set memory limit protection for odemisd"
    echo "Ensure you have odemisd cgroup available"
    # that should do it:
    # sudo cgcreate -a $USER -g memory:odemisd
fi


export PYTHONPATH
$RUNNER ${PYTHON_INTERPRETER:-/usr/bin/python} -m $BACKEND "$@"
