Qubes Auto Credit2: automatically set xen credit2 scheduling priority

Hello everyone!

Posting another one of my scripts that I use to make Qubes snappier and more usable interactively :slight_smile:

This one makes makes use of the credit2-weight feature to automatically set credit2 scheduler weight (scheduling priority) for VMs.

Enjoy! And thanks to @noskb and @anon40243023 for providing the idea for the Admin Events approach.

6 Likes

This works quite well, thanks!

I have proposition for more universal script, that modifies xen scheduler weight of any xen scheduler:

#!/usr/bin/env python3
# auto-xen-weight.py script to automatically modify default
# Xen Scheduler weight for any qube with `xen-sched-weight`
# feature for any Xen Scheduler

import asyncio
import subprocess

import qubesadmin
import qubesadmin.events

feat_sched = "xen-sched-weight"
sched_check = [ 'xl', 'info', 'xen_scheduler' ]

def capture_os_cmd(cmd):
   with subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) as proc:
       output, _ = proc.communicate()
   return output.decode().strip()

def _sched_weight(sched, name, weight):
   cmd = ['xl', 'sched-'+sched, '-d', name, '-w', weight]
   subprocess.run(cmd).check_returncode()

def sched_by_feat(vm, event, **kwargs):
   vm = app.domains[str(vm)]
   sched = capture_os_cmd(sched_check)
   if feat_sched in list(vm.features):
       try:
           weight = int(vm.features[feat_sched])
           _sched_weight(sched, vm.name, str(weight))
           print(f'Set {vm.name}\'s {sched} weight to {weight}')
       except:
           pass

sched = capture_os_cmd(sched_check)
_sched_weight(sched, "Domain-0", "2048")
app = qubesadmin.Qubes()
dispatcher = qubesadmin.events.EventsDispatcher(app)
dispatcher.add_handler('domain-start', sched_by_feat)
asyncio.run(dispatcher.listen_for_events())

PS: @Atrate look at your github - ther’s more to that but I don’t want to spam it here on forum

1 Like

I forgot to announce it in this thread, but the script now (since 3 weeks ago) also handles -dm domains automatically.

I’m also working on a fix to a weird bug (race condition?) that caused domains sometimes not to be adjusted correctly. I’ve deployed the fix locally and I’m testing it, will update the repo and this thread once it’s confirmed that it works.

1 Like

Update: I’ve fixed a bug that caused some domains not to get their priorities adjusted correctly, some of the time. The update is live on Github.