Snap Quota Groups

A snap quota group is used put some resource limitations to snap packages.

My use case

I was frustrated with how pycharm kept freezing my machine every now and then, sometimes for 5 minutes sometimes for 20 minutes, sometimes I just had to reboot it. As this was happening more and more often I had began to think about different alternatives. Invest myself totally in vscode, make the final move to a terminal base editor. This was not a new problem to me, back in 2015 I had a mac computer which could not run my editor of the time (atom), so might be I just to find a way to stick to the same editor for more than 5 years. I did some small research (and by that I mean I spent 30 mins in google), and found out that pycharm indexing feature wants to use 100% of your cpu. I was monitoring now my pc and found out that this was the root problem. Cool finally something I can act on. You can actually turn off the indexing functionality, but you will loose the search functionality. Not an option.

About this time, we were running into a similar problem in production. Some of our services were eating more hardware resources that they should and were killing our servers. Solution, use cgroups.

Thinking about it, I could use cgroups to restrict any resources for any software I run. Might be to teddious. Some more research and found a post on hacker news talking about a snap experimental feature, quota groups.

You can read more about them in their docs, but it basically allows to restrict log, memory and cpu usage for any group of snaps. Just what I needed.

Setting up quota groups

First you need to enable this feature, just run:

$ sudo snap set system experimental.quota-groups=true

Now you have to decide how much resources these snaps are allow to use. My machine has 8 cores and 16GBs of ram, so something less than that. I must say that I had to lower the cap after my initial choice.

Create a quota:

$ sudo snap set-quota cpu-eaters --memory=12GB --cpu=8x80% pycharm-professional

Check out your quota groups

You can see your quota groups and their allowances with:

$ sudo snap quotas

Now you can see your quota group:

Quota       Parent  Constraints              Current
cpu-eaters          memory=12.0GB,cpu=8x80% 

If you want to see which snaps are inside each group:

$ sudo snap quota cpu-eaters

Which produces

name:  cpu-eaters
constraints:
  memory:          12.0GB
  cpu-count:       8
  cpu-percentage:  80
current:
  memory:  0B
snaps:
  - pycharm-professional

Updating quotas group

After few days of setting the quota group I had to reduce the number of cores and also throw slack into the group so I run:

$ sudo sudo snap set-quota cpu-eaters --cpu=6x80% slack

Then I checked the quotas and could see they were updated:

name:  cpu-eaters
constraints:
  memory:          12.0GB
  cpu-count:       6
  cpu-percentage:  80
current:
  memory:  0B
snaps:
  - slack
  - pycharm-professional

Closing thoughts

It has been few months since I have implemented the solution and I am quite happy with it. It took me less than 5 minutes to set it up, no need to replace any software and just works. My machine is usable once again.