LinHES Forums
http://forum.linhes.org/

Pause Commflagging Script
http://forum.linhes.org/viewtopic.php?f=3&t=16027
Page 1 of 1

Author:  brfransen [ Thu Aug 02, 2007 5:28 pm ]
Post subject:  Pause Commflagging Script

On my 3 GHz P4 when I am playing back an HD (either 720p or 1080i) program and commflagging I will get some studdering. I have tried the babysitting script to renice commflagging to 19 and it helps some but didn't completely solve my problem. I found that I had to either schedule commflagging for later or manually pause it while I was watching HD. I found many times that I would forget to restart commflagging after watching a program. To solve this I wrote a script that will check the cpu usage and if it is above a certain threshold it will pause the commflagging process. If the cpu usage is below the threshold it will continue commflagging. I run this script every minute from crontab and it seems to work well. I have played with the threshold value and 65 has worked well for me but that may need to be tweaked for different systems.

Here is the script. If you do put this in your crontab with logging I would suggest adding the following code to /etc/logrotate.d/pause_commflag
Code:
/var/log/mythtv/pause_commflag.log {
        daily
        rotate 7
        notifempty
        copytruncate
        compress
}

I am new to Linux and shell scripting in general so if there are any glaring issues, please speak up.

Thanks,
Britney

Author:  crushinator [ Sun Sep 02, 2007 6:09 pm ]
Post subject: 

I remembered reading about this script earlier, and was interested in it because, like you, the "renice" technique worked a little but not all the way for me. So I searched for "pause mythcommflag" but couldn't find it. Reason why? Because this post doesn't actually contain the word "mythcommflag!" So now it does. :)

Incidentally, has anyone else used this pause script with success?

Author:  cecil [ Sun Sep 02, 2007 6:46 pm ]
Post subject: 

Thanks, both the script and log rotate will be in the next release for those that choose to use it.

Author:  crushinator [ Sun Sep 09, 2007 2:10 am ]
Post subject: 

Thank you for this pause commflag script. I like this script, and I made these changes:

1.
Code:
FRONTENDCPU=`top -b -n 1 -u mythtv | grep mythfrontend | cut -c 43-45`

I had to change "43-45" to "42-45" to get the entire %CPU usage (##.#), e.g. 48.5%. Otherwise it only picked up the end digits (#.#), e.g. 8.5%.

2.
Code:
if [ "$FRONTENDCPU" -ge "$CPUTHRESHOLD" ]

I think this expression to check "greater than or equal" was working okay at first, but after I was tweaking the code, it began to give me error "integer argument expected." So instead, after asking the Internet, I changed it by adding a second set of brackets and using ">" sign, which converted this test command into an extended test command. (http://www.museum.state.il.us/ismdepts/ ... ructs.html ) I think the problem is that the original conditional statement couldn't handle decimal values, though I don't know why.
Code:
if [[ "$FRONTENDCPU" > "$CPUTHRESHOLD" ]]


3. I added some hyphens and spaces in front of the "echo" lines to make the /var/log/mythtv/pause_commflag.log more readable (to me).

4. The original script decides to pause mythcommflag by looking at mythfrontend %CPU usage instead of total CPU usage. I preferred to look at total CPU usage because I don't need to pause mythcommflag every time I watch something, only when the total exceeds my CPU limits. But every time I grabbed that number programmatically, it said "10.4%" even though "top" visually showed me 80-90%. So instead I grabbed LOAD AVERAGE OVER ONE MINUTE and set a threshold of 3.00.
Code:
#!/bin/bash
# pause_commflag.sh   v0.1.1
# Utility to automatically pause & unpause mythcommflag if CPU usage is above a certain level
# Free for any use.
# Installation:
#  cp pause_commflag.sh /usr/local/bin
#  chmod +x /usr/local/bin/pause_commflag.sh
# Usage: Add to crontab (crontab -e) with logging:
# * * * * * /usr/local/bin/pause_commflag.sh >>/var/log/mythtv/pause_commflag.log 2>&1
# Usage: Add to crontab (crontab -e) without logging:
# * * * * * /usr/local/bin/pause_commflag.sh


NOWDATE=$(date)
LOADAVERAGETHRESHOLD=3.00
PROCCOMMFLAG=`pidof mythcommflag`
if [ -n "${PROCCOMMFLAG}" ]
then

        echo "-$NOWDATE   COMMFLAG Process FOUND. Checking Load Average over 1 minute."
        LOADAVERAGE=`top -b -n 1 -u mythtv | grep load\ average | cut -c 59-62`
        if [[ "$LOADAVERAGE" > "$LOADAVERAGETHRESHOLD" ]]
        then
                echo "  LOAD AVERAGE over 1 minute ($LOADAVERAGE) is greater than the threshold \
($LOADAVERAGETHRESHOLD). PAUSE Commflagging"
                kill -s STOP $PROCCOMMFLAG
        else
                echo "  LOAD AVERAGE over 1 minute ($LOADAVERAGE) is less than the threshold \
($LOADAVERAGETHRESHOLD). CONTINUE Commflagging"
                kill -s CONT $PROCCOMMFLAG

        fi
#else
#       echo "No COMMFLAG Process Active"
fi



My computer setup is 1.6 GHz with 1 GB RAM with ext3 LVM with PVR-150 and WinTV-GO (bttv) and so I have to be careful when simultaneously recording to MPEG-4 via software encoding plus watching an MPEG-4 recording plus mythcommflagging. It "works" but with some recording or playback glitches especially "on the half-hour or hour" when another recording is finishing and mythcommflag is starting up. I am going to post a new topic asking for your suggestions on how best to manage all these. Thanks.

P.S. there is another method given by eweaver that adjusts io priority of mythcommflag. Is anyone familiar about this?

Author:  brfransen [ Sun Sep 09, 2007 8:50 am ]
Post subject: 

I am glad that you were able to modify this to work for you. I am running R5F1 and am using the version top that came with it. Are you using a different version of KnoppMyth? I am curious as to why you are getting a different output.

One of the reasons I looked at the mythfrontend CPU usage instead of overall CPU is that I thought that commflagging itself would contribute to high CPU load and might trigger a pause when only commflagging was consuming CPU. Have you experienced this or does the Load Average for 1 minute not rise too high with just commflagging?

The other method you linked is similar to the babysitting script I linked above but it appears to change the priority of IO. I have not tried it.

Britney

Author:  crushinator [ Sun Sep 09, 2007 6:40 pm ]
Post subject: 

brfransen wrote:
I am running R5F1 and am using the version top that came with it. Are you using a different version of KnoppMyth? I am curious as to why you are getting a different output.

I am using R5F1 also, with the default "top" version.

Quote:
One of the reasons I looked at the mythfrontend CPU usage instead of overall CPU is that I thought that commflagging itself would contribute to high CPU load and might trigger a pause when only commflagging was consuming CPU. Have you experienced this or does the Load Average for 1 minute not rise too high with just commflagging?

For me, commflagging by itself is not too much of a load, and even having some combination of commflagging and recording or commflagging and playback might push the load average to around 2.8, which is why I set my threshold at 3.0. But only when recording on bt878 software encoding plus playback plus mythcommflag, I will see load averages around 4 to 5 which will pretty much screw up the recording plus give glitchy playback.

I think this "overall" threshold is better than only looking at mythfrontend usage. Although note that I play standard-def TV, not high-def TV, so I have a little more headroom.

Quote:
The other method you linked is similar to the babysitting script I linked above but it appears to change the priority of IO. I have not tried it.

I haven't tried it either, but I am intrigued by it. I found a post on the MythTV mailing list that suggests putting the "ionice" function directly into the mythcommflag command in mythtv-setup. This would eliminate using crontab, and seems like it would be more immediate.

Page 1 of 1 All times are UTC - 6 hours
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/