You are not logged in.

#1 2012-04-16 19:01:44

drtechie
Member
Registered: 2012-02-29
Posts: 5

Help with a "cpulimit" script

Hello guys,

I am not sure whether my post has gone to the right section.
Its about a small script that I need.

Ok here is my issue. I am a student and in my department I have setup a PC (which is an old P4 system) which acts as a web server for a forum that can be accessed via LAN. The same PC hosts a Counter Strike Server which some 8 of us play at night for 1-2 hrs. So most of the time the system is idle. Well , then I thought I will use it for a good cause and installed folding@home , the distributed computing project. Now my counter strike server when idle uses 15.4% of CPU. The folding@home client uses more than 75% CPU. Now the problem is that after installing folding@home Counter Strike server FPS dropped and its unplayable.

I came along "cpulimit" which can decrease the percentage of usage for a specific program. So what I need is daemon/ script which

1. checks pid of 'hlds' (Counter Strike Server) and folding@home.
1. checks its usage
2. If usage of 'hlds' spikes above, lets say 17%, which means game has begun, the folding@home cpu usage should be brought down to less than 5%.
3. When the game is over and hlds comes back to <16% usage the folding@home client should throttle back to maximum and run for rest of the day.

I have been using Linux for 2 years but I have absolutely no idea of bash scripting. Can some kindly help how to tackle the above issue.

Thanks and Regards
--drtechie--

Offline

#2 2012-04-17 01:29:21

tavianator
Member
From: Waterloo, ON, Canada
Registered: 2007-08-21
Posts: 859
Website

Re: Help with a "cpulimit" script

$ man nice

Last edited by tavianator (2012-04-17 01:29:27)

Offline

#3 2012-04-17 04:20:27

dodo3773
Member
Registered: 2011-03-17
Posts: 818

Re: Help with a "cpulimit" script

Not 100% sure this will work but it may give you some ideas:

#!/bin/bash
while true; do
sleep 5
w=$( ps aux | sed /grep/d | grep -o cpulimit)
x=$( ps aux | sed /grep/d | grep gameserverprocessname )
y=$( ps aux |  sed /grep/d | grep foldprocessname )
z=$( ps aux | sed /grep/d | grep gameserverprocessname | awk '{print $3}' )
if [[ "$x" && "$y" && "$z" > "17.0" && "$w" != "cpulimit" ]]; then 
cpulimit -l 5 -p $(pidof foldprocessname) -z
fi
done

of course foldprocessname is whatever ps aux gives you for that folding@home whatever program and gameserverprocessname would have to be replaced with counter strike server process name. If foldprocessname has more then one pid then this:

cpulimit -l 5 -p $(pidof foldprocessname) -z

will definitely not work.

The above code is probably a little (a lot) sloppy and I am sure it can be improved greatly. Hopefully it can give you a point in the right direction though.

Offline

#4 2012-04-17 16:03:43

drtechie
Member
Registered: 2012-02-29
Posts: 5

Re: Help with a "cpulimit" script

dodo3773, Thanks a lot for that informative reply..

I modified the code a bit.

w=$( ps aux | sed /grep/d | grep -o cpulimit)

cpulimit is not running at boot and i didn't understand this.

And moreover

ps aux | sed /grep/d | grep hlds_amd
ps aux | sed /grep/d | grep FahCore_a4.exe

didn't output any numbers, but

ps aux | sed /grep/d | grep hlds_amd | awk '{print $3}'

did give me the %CPU.

And second thing is, I want to bring down usage of FahCore_a4.exe (folding@home) when %CPU usage of hlds_amd (gameserver) goes up a specific %. I made a smaller script and made it run by editing rc.local

The script

#!/bin/bash
while true; do
sleep 3
z=$ (ps aux | sed /grep/d | grep hlds_amd | awk '{print $3}' )
if [["$z" > "24.0" ]]; then
cpulimit -l 5 -p $ {pidof FahCore_a4.exe) -z
fi
done

This code does run and decreases usage of FahCore_a4.exe to 5% when hlds_amd usage goes above 24%
But with a small glitch. Once the cpulimit is applied the FahCore_a4.exe doesn't come back to normal even though hlds_amd usage goes below 24%.

I guess I am doing something wrong.

Offline

#5 2012-04-17 18:53:25

dodo3773
Member
Registered: 2011-03-17
Posts: 818

Re: Help with a "cpulimit" script

This:

w=$( ps aux | sed /grep/d | grep -o cpulimit)

is to make sure cpulimit is not already running so you don't call that command over and over in your loop (you may have to backround cpulimit in the script though with "&").

These:

ps aux | sed /grep/d | grep hlds_amd
ps aux | sed /grep/d | grep FahCore_a4.exe

Are to verify that both processes are running.

One thing I did forget to throw in there is another if statement to kill cpulimit if the process goes back below whatever cpu %. It should be simple. All you have to do is go  $z" < "24.0". Just make sure to put it in the same while loop (before done and after fi) and you should be fine.

Offline

Board footer

Powered by FluxBB