views:

1317

answers:

7

I want to run Folding@home client on my Ubuntu 8.10 box only when it's idle because of the program's heavy RAM consumption.

By "idle" I mean the state when there's no user activity (keyboard, mouse or any other else). It's ok for other (probably heavy) processes to run at that time since F@H has the lowest CPU priority. The point is just to improve user experience and to do heavy work when the he is away.

How to accomplish this?

+1  A: 

The file /proc/loadavg has the systems current load. You can just write a bash script to check it, and if its low then run the command. Then you can add it to /etc/cron.d to run it periodically.

This file contains information about the system load. The first three numbers represent the number of active tasks on the system - processes that are actually running - averaged over the last 1, 5, and 15 minutes. The next entry shows the instantaneous current number of runnable tasks - processes that are currently scheduled to run rather than being blocked in a system call - and the total number of processes on the system. The final entry is the process ID of the process that most recently ran.

Example output:

0.55 0.47 0.43 1/210 12437
zodeus
Loads do not show heavy overcommitment, swap usage, etc. They aren't a silver bullet for this type of decision in a program.
Tim Post
+4  A: 

It's fiddly to arrange for the process to only be present when the system is otherwise idle.

Actually starting the program in those conditions isn't the hard bit. You have to arrange for the program to be cleanly shut down, and figure out how and when to do that.

You have to be able to distinguish between that process's own CPU usage, and that of the other programs that might be running, so that you can tell whether the system is properly "idle".

It's a lot easier for the process to only be scheduled when the system is otherwise idle. Just use the 'nice' command to launch the Folding@Home client.

However that won't solve the problem of insufficient RAM. If you've got swap space enabled, the system should be able to swap out any low priority processes such that they're not consuming and real resources, but beware of a big hit on disk I/O each time your Folding@Home client swaps in and out of RAM.

p.s. RAM is very cheap at the moment...

p.p.s. see this article

Alnitak
How is this 'fiddly' if writing something like BOINC? :)
Tim Post
What IS fiddly is not anticipating the state of the system once you're done, which I tried to answer.
Tim Post
Thanks for your answer, Alnitak. I've clarified the meaning of "idle" in the question.
wheleph
+6  A: 

When the machine in question is a desktop, you could hook a start/stop script into the screensaver so that the process is stopped when the screensaver is inactive and vice versa.

Jan Jungnickel
xscreensaver allows to make it by means of xscreensaver-command -watch (see http://www.jwz.org/xscreensaver/faq.html#watch)
wheleph
Article on that: http://wheleph.blogspot.com/2009/09/running-of-foldinghome-clients-when.html
wheleph
+2  A: 

Your going to want to look at a few things to determine 'idle' and also explore the sysinfo() call (the link points out the difference in the structure that it populates between various kernel versions).

Linux does not manage memory in a typical way. Don't just look at loads, look at memory. In particular, /proc/meminfo has a wonderful line started with Committed_AS, which shows you how much memory the kernel has actually promised to other processes. Compare that with what you learned from sysinfo and you might realize that a one minute load average of 0.00 doesn't mean that its time to run some program that wants to allocate 256MB of memory, since the kernel may be really over-selling. Note, all values filled by sysinfo() are available via /proc, sysinfo() is just an easier way to get them.

You would also want to look at how much time each core has spent in IOWAIT since boot, which is an even stronger indicator of if you should run an I/O resource hog. Grab that info in /proc/stat, the first line contains the aggregate count of all CPU's. IOWAIT is in the 6'th field. Of course if you intend to set affinity to a single CPU, only that CPU would be of interest (its still the sixth field, in units of USER_HZ or typically in 100'ths of a second). Average that against btime, also found in /proc/stat.

In short, don't just look at load averages.

EDIT

You should not assume a lack of user input means idle.. cron jobs tend to run .. public services get taxed from time to time, etc. Idle remains your best guess based on reading the values (or perhaps more) that I listed above.

EDIT 2

Looking at the knob values in /proc/sys/vm also gives you a good indication of what the user thinks is idle, in particular swappiness. I realize your doing this only on your own box but this is an authoritative wiki and the question title is generic :)

Tim Post
Thanks for interesting ideas, tinkertim. I've used word "idle" in another meaning which I've clarified in the question.
wheleph
Given cron and other things, you can not describe idle in that way. A lack of user input does NOT mean idle, especially when 'updatedb' runs in the middle of the night.
Tim Post
you'll note that before you edited he clarified that he's not bothered about other 'background' tasks, only interactive response.
Alnitak
+1  A: 

may be You need to set on idle task lowest priority via nice.

vitaly.v.ch
This is actually the way you should do this on Linux.
Arafangion
The problem with process priority is that it is only CPU and these days disk is often the main problem
Ian Ringrose
topicstarter talk about heave RAM usage but none about disk usage
vitaly.v.ch
@vitaly.v.ch, as far as I know 'nice' changes the amount of CPU time that is consumed by a process versus other processes. I don't get how this is related to the amount of RAM it uses.
wheleph
A: 

See this thread for a perl script that checks when the system is idle (through gnome screensaver).
You can run commands when idling starts and stops.
I'm using this with some scripts to change BOINC preferences when idling
(to give BOINC more memory and cpu usage).

perl script on ubuntu forums

+1  A: 

If you're using Gnome then take look at this:

http://live.gnome.org/GnomeScreensaver/FrequentlyAskedQuestions#head-ac43c8f33bc700a5e298e6a82ded0e8bb9b33043

Strudel