You are not logged in.

#1 2011-10-12 07:38:10

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,397

wmnice: a window manager aware process renicer.

Here is a little gambas script which makes the current window prioritized.
When the active window changes, an event is triggered, then it manages to discover the pid of the current application and renice it with "Current nice - nicestep" value (default -5).
The previous active window is then restored to the previous nice value (if it was reniced).

To exit the script, you have to hit the ENTER key so that it can restore the last window pid (or else it is left 'niced')

Also, you probably need to run [EDIT] with suid bit set or give your user the rights to set priorities under 0[/EDIT]

There is no configuration, all you need is to run the script.

Required packages:
* gambas3-gb-gui
* gambas3-gb-image
* gambas3-gb-desktop
* wmctrl

' Gambas module file

Public Extern getpriority(which As Integer, who As Integer) As Integer In "libc:6"
Public Extern setpriority(which As Integer, who As Integer, nice As Integer) As Integer In "libc:6"
Public MyDesktopWatcher As Desktopwatcher 

Public LastWindowName As String
Public LastPid As Integer = -9999

Private NiceStep As Integer = 5 'niceness step value

Private Function PidFromWid(Wid As String) As Integer
  Dim wmctrl As String
  Shell "wmctrl -lp | grep " & wid To wmctrl
  Print "-------------------"
  Return Trim(Mid(wmctrl, 15, 6))
End

Public lastnice As Integer = -9999

Public Sub MakeFasterAndRestore(pid As Integer, lastpid As Integer)
  'Restore last pid nice
  If ((lastnice > -9999) And (lastpid <> -9999)) Then
    setpriority(0, lastpid, lastnice)
    Debug "RESTORE: Setpriority OK, PID=", lastpid, "NewPri=", getpriority(0, lastpid)
  Endif
  'Get the current pid nice and store it
  lastnice = getpriority(0, pid)
  'make the current pid faster
  setpriority(0, pid, lastnice - NiceStep)
  Debug "FASTER : Setpriority OK, PID=", pid, "NewPri=", getpriority(0, pid)
End

Public Sub Restore(pid As Integer)
  If lastnice = -9999 Then Return
  setpriority(0, pid, lastnice)
  Debug "RESTORE: Setpriority OK, PID=", pid, "NewPri=", getpriority(0, pid)
End

Public Function ActiveWid() As String
    Return Lower(Hex(Desktop.activewindow))
End

Public Sub MyDesktopWatcher_activewindow()
  'raised when the active window has changed
  Dim ActiveWindowName As String
  Dim pid As Integer

  'avoid double events:
  Try ActiveWindowName = Desktop.Windows.FromHandle(Desktop.ActiveWindow).Name
  If Error Then Return
  If ActiveWindowName = LastWindowName Then Return
  lastwindowname = ActiveWindowName

  Try PID = PidFromWid(ActiveWid())
  If Error Then Return

  Debug "Active window caption= ", ActiveWindowName
  Debug "PID", pid

  If (pid <> "") Then
     MakeFasterAndRestore(pid, lastpid) 'renice current pid and restore previous
  Endif

  LastPid = pid  
  Print "** hit ENTER to quit, NOT CTRL-C **"
End

Public Sub Application_read() 'ENTER key to exit
  'restore last pid with priority from lastnice (global var)
  Restore(PidFromWid(ActiveWid()))
  Print "bye"
  Quit
End

Public Sub Main()
  MyDesktopWatcher = New DesktopWatcher(True) As "MyDesktopWatcher"
End

Last edited by kokoko3k (2011-10-12 17:38:33)


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#2 2011-10-12 09:47:19

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,397

Re: wmnice: a window manager aware process renicer.

To achieve better responsiveness, i'm also using FIFO or RoundRobin realtime scheduling policy.
It took me a while to understand why:
sysctl -w  kernel.sched_rt_runtime_us=-1
...was needed to avoid chrt's "operation not permitted" error


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

#3 2011-10-12 13:36:03

avx
Member
Registered: 2011-07-05
Posts: 71

Re: wmnice: a window manager aware process renicer.

Interesting concept, have to try that out sometime. Though I won't use your implementation, too much/uncommon deps imho. I'm sure this concept could be quite easily done directly with some WMs, i.e. FVWM (via the Event-Intercafe).

But I'll do it less generic, i.e. I don't see the point giving f.e. my mail-client a high priority when there's things like video conversion running in the background.

There's also verynice, which I found by a quick search.

Thanks for the idea, though smile

Offline

#4 2011-10-12 16:14:47

kokoko3k
Member
Registered: 2008-11-14
Posts: 2,397

Re: wmnice: a window manager aware process renicer.

Gambas is just a quick scripting language, and wmctrl is an utility to control window managers (i use it to get the application name, is not strictly required, but handy).

The idea is to have a more responsive desktop, i'd renice even the email client, because i want it -say- to scroll smoothly, and if it isn't doing anything, renicing it even to -20 or realtime doesn't impact on background processes at all.


Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !

Offline

Board footer

Powered by FluxBB