source "$EXP_ROOT/experiments/perf.shared.sh"

CONNS=160 # per agent thread

NO_RUNS=0

REINIT_RUNS=4

load_config() {
    source "$EXP_ROOT/configs/memcached.sh"
    source "$EXP_ROOT/configs/mutilate.sh"
}

cleanup_mutilate() {
    echo "Killing all mutilate processes on clients"
    sudo -u $SSH_USER pdsh -R ssh -w $MEMCLIENTS pkill mutilate || true
}

cleanup_memcached() {
    echo "Killing memcached process"
    kill -INT $MEMCACHED_PID || true
    sleep 2
    kill $MEMCACHED_PID || true
}

start_memcached() {
    echo "Starting memcached"
    taskset -c 0-$((THREADS - 1)) $MEMCACHED -u root -t $THREADS -b 16384 -c 32768 -m 10240 -o hashpower=24,no_lru_maintainer,no_lru_crawler $MEMCACHED_ARGS 2>&1 > /tmp/memcached.lastexp.log &
    MEMCACHED_PID=$!
}

start_mutilate() {
    echo "Starting mutilate"
    # Mutilate client agents always run on 8 cores
    sudo -u $SSH_USER pdsh -R ssh -w $MEMCLIENTS taskset -c 0-7 mutilate -A -T 8 2>/dev/null & sleep 1
}

warmup() {
    echo "Warming up Memcached..."
    sudo -u $SSH_USER $MUTILATE --loadonly
    sudo -u $SSH_USER $MUTILATE --noload -c100 $MUTCLIENTS -t30 -u 0
}

run_mutilate() {
    if [ "$NO_RUNS" -ge "$REINIT_RUNS" ]; then
        echo "Re-intializing Memcached server"
        memcached_shutdown
        memcached_startup
        NO_RUNS=0
    fi
    echo "Running mutilate with $CONNS connections per agent (core) and QPS = $QPS"
    perf_stat_start "$1"
    sudo -u $SSH_USER $MUTILATE --noload -c$CONNS $MUTCLIENTS -t30 -u 0 -q $QPS | tee $DATA_OUT/$1.txt
    perf_stat_stop
    NO_RUNS=$((NO_RUNS + 1))
}

memcached_pre_start() {
    load_config

    if [ "$FSTACK" == "true" ] && [ $THREADS -gt 1 ]; then
        echo "Cannot run F-Stack memcached in multi-threaded mode"
        exit 1
    fi

    if [ "$FSTACK" == "true" ]; then
        MEMCACHED="$MEMCACHED_FSTACK"
    fi

    EXPERIMENT_NAME_EXT="t$THREADS.c$CONNS"

    if [ "$NAPI_LOCALITY" == "true" ]; then
        MEMCACHED_ARGS="$MEMCACHED_ARGS -N $THREADS"
    fi

    pkill memcached || true
    sleep 1
    cleanup_mutilate    
}

memcached_init() {
    NO_RUNS=0
    memcached_startup
}

memcached_startup() {
    start_memcached
    start_mutilate
    sleep 15
    warmup
}

memcached_shutdown() {
    cleanup_mutilate
    cleanup_memcached
}

memcached_deinit() {
    memcached_shutdown
}

