#!/bin/sh
#-------------------------------------------------------------------------+
# Copyright (C) 2016 ramdaron (https://github.com/ramdaron)
# Copyright (C) 2016 Matt Churchyard (churchers@gmail.com)
# All rights reserved
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted providing that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

# set limits to virtual machine
# this is the background process
#
rctl::set(){
    local _pcpu _rbps _wbps _riops _wiops
    local _pid _pri

    # get limit settings
    config::get "_pri" "priority"
    config::get "_pcpu" "limit_pcpu"
    config::get "_rbps" "limit_rbps"
    config::get "_wbps" "limit_wbps"
    config::get "_riops" "limit_riops"
    config::get "_wiops" "limit_wiops"

    # wait till bhyve starts and get pid
    sleep 1
    _pid=$(pgrep -fx "bhyve[: ].* ${_name}")
    [ -z "${_pid}" ] && return 1

    # check for a priority
    [ -n "${_pri}" ] && renice ${_pri} ${_pid} >/dev/null 2>&1

    # return if there are no limits
    [ -z "${_pcpu}${_rbps}${_wbps}${_riops}${_wiops}" ] && return 1

    # see if rctl works
    /usr/bin/rctl >/dev/null 2>&1
    [ $? -ne 0 ] && \
        util::log "guest" "${_name}" "RCTL support requested but RCTL not available" && return 1

    util::log "guest" "${_name}" "applying rctl limits"

    if [ -n "${_pcpu}" ]; then
        /usr/bin/rctl -a process:${_pid}:pcpu:deny=${_pcpu} >/dev/null 2>&1
        [ $? -eq 0 ] && util::log "guest" "${_name}" " pcpu=${_pcpu}"
    fi

    # at this point we can return if < FreeBSD 11
    [ ${VERSION_BSD} -lt 1100000 ] && return 0

    if [ -n "${_rbps}" ]; then
        /usr/bin/rctl -a process:${_pid}:readbps:throttle=${_rbps} >/dev/null 2>&1
        [ $? -eq 0 ] && util::log "guest" " readbps=${_rbps}"
    fi

    if [ -n "${_wbps}" ]; then
        /usr/bin/rctl -a process:${_pid}:writebps:throttle=${_wbps} >/dev/null 2>&1
        [ $? -eq 0 ] && util::log "guest" " writebps=${_wbps}"
    fi

    if [ -n "${_riops}" ]; then
        /usr/bin/rctl -a process:${_pid}:readiops:throttle=${_riops} >/dev/null 2>&1
        [ $? -eq 0 ] && util::log "guest" " readiops=${_riops}"
    fi

    if [ -n "${_wiops}" ]; then
        /usr/bin/rctl -a process:${_pid}:writeiops:throttle=${_wiops} >/dev/null 2>&1
        [ $? -eq 0 ] && util::log "guest" " writeiops=${_wiops}"
    fi
}
