Prioritise X applications with focus

nicepriorityscheduling

Is there a way to prioritise X applications with focus, preferably both in terms of CPU and I/O?

Something like all X applications are niced and ioniced down in priority, but when they gain focus they get niced and ioniced back up to normal levels.

I don't want to reinvent the wheel, so I was hoping someone had already developed an application that does this.

Best Answer

I wrote a small script in perl to do what you ask: http://pastie.org/3460943

It works by getting all the current X11 window IDs using the "xlsclients -l" command, in turn getting the window's PID with xprop. It then uses xprop -root to get the currently focused window, then loops through all the PIDs to change their nice value to 5 (keeping the currently focused one at 0.)

It then enters an infinite loop that re-checks the focused window every two seconds. If the window focus has changed, it changes the old one's nice value to 5 and the new one to 0. Also, every 40 seconds it redoes what it did in the first paragraph so new processes can be prioritized.

This script has several flaws, the most obvious of which is the fact that xprop sometimes fails to find the currently focused window (this seems to happen only if you're switching between windows really fast.) Also, if you're running a window from a network location, the PID might not correlate to the actual PID on your system. That said, on average the script will work (perl can handle a few errors and still run)

I also think it would be faster if I wrote this in c, but I have no skill in using the X11 library, so bash-filled perl it is!