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 @renehoj for providing the idea for the Admin Events approach.

4 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