summaryrefslogtreecommitdiffstats
path: root/abs/core-testing/linhes-scripts/limit-mythcommflag.sh
blob: 5cd81bbe6971a48ae0bc945c8c1b8d44dd86e9fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/bin/bash
# limit-mythcommflag.sh   v0.2  03/2/10
# Utility to automatically limit mythcommflag if CPU system load is above a certain level
# and if backend usage is above a certain level
# Uses cpulimit from http://cpulimit.sourceforge.net/
# Free for any use.
# Installation:
#  cp limit-mythcommflag.sh /usr/LH/bin
#  chmod +x /usr/LH/bin/limit-mythcommflag.sh
# Usage: Executed from runit limit-mythcommflag at boot

#-----OPTIONS______
# Number of seconds to wait between checking for mythcommflag process
SLEEP=5
# Backend and Frontend thresholds above which mythcommflag is stopped
BETHRESHOLD=30
FETHRESHOLD=55

# FUNCTIONS
limit_cpu()
# Arg_1 = LimitedRangeNum; Arg_2 = CPULimitPercent; Arg_3 = Commflag PID
{
if [ $ALREADYLIMITED -ne $1 ] ; then
    if [ $cpulimit_pid -ne 0 ]; then
        kill $cpulimit_pid
    fi
    cpulimit -p $3 -l $2 &
    cpulimit_pid=$!
    ALREADYLIMITED=$1
fi
}

stop_limit()
{
if [ $ALREADYLIMITED -ne 0 ]; then
    if [ $cpulimit_pid -ne 0 ]; then
        kill $cpulimit_pid
        cpulimit_pid=0
    fi
    ALREADYLIMITED=0
fi
}

ALREADYLIMITED=0
STOPPED=0
cpulimit_pid=0

while true; do
    PROCCOMMFLAG=`pidof mythcommflag`
    if [ -n "${PROCCOMMFLAG}" ]; then
        BACKENDCPU=`top -bn1u mythtv | grep -m 1 mythbackend | awk '{ print $9 }'`
        FRONTENDCPU=`top -bn1u mythtv | grep -m 1 mythfrontend | awk '{ print $9 }'`
        if [ "$FRONTENDCPU" = "" ]; then
            FRONTENDCPU=0
        fi
        if [ "$BACKENDCPU" -ge "$BETHRESHOLD" ]; then
            if [ "$FRONTENDCPU" -ge $FETHRESHOLD ]; then
                if [ $STOPPED -eq 0 ]; then
                    kill -s STOP $PROCCOMMFLAG
                    STOPPED=1
                fi
            else
                if [ $STOPPED -eq 1 ]; then
                    kill -s CONT $PROCCOMMFLAG
                    STOPPED=0
                fi
                stop_limit
            fi
        else
            if [ $STOPPED -eq 1 ]; then
                kill -s CONT $PROCCOMMFLAG
                STOPPED=0
            fi
            case $FRONTENDCPU in
	        [0-9]|[0-3][0-9] )
                stop_limit;;
	        [4-5][0-9] ) 
                limit_cpu "1" "70" $PROCCOMMFLAG;;
	        [6-7][0-9] ) 
                limit_cpu "2" "50" $PROCCOMMFLAG;;
	        [8-9][0-9] ) 
                limit_cpu "3" "20" $PROCCOMMFLAG;;
	        [1][0-9][0-9] )
                limit_cpu "4" "10" $PROCCOMMFLAG;;
	        esac
        fi
#    else
#        echo "No COMMFLAG Process Active"
    fi
    sleep $SLEEP
done