summaryrefslogtreecommitdiff
path: root/content/posts/2021-02-03-gentoo-no-desktop-lag.md
blob: 966419b2a499501ae3b23435397736da6dadcd81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
---
layout: post
title: Say goodbye to desktop lag while compiling your @world
category: GNU/Linux
tags:
  - gentoo
  - systemd
---

1. Start by creating a new systemd slice `/etc/systemd/system/portage.slice`:

   ```
   [Install]
   WantedBy=slices.target

   [Slice]
   CPUShares=256
   ```

1. Enable and start the unit you just created:

   ```
   systemctl enable --now portage.slice
   ```

   CPUShares option defaults to 1024, `systemd` will create a user slice for
   each user with an active session, and all processes that user run will be
   assigned to that slice, anything that a user may run will receive 4 times the
   CPU time of processes assigned to the portage slice.

   ```
   ➜  ~ cat /sys/fs/cgroup/cpu,cpuacct/user.slice/cpu.shares
   1024
   ```

1. Repurpose `PORTAGE_IONICE_COMMAND` variable. This is one of those awesome
   variables you can set in your `make.conf` to alter how you build stuff. It
   should be a command string for portage to call to modify its own priority
   with a `\${PID}` placeholder that will be substituted with a `PID`. Maybe it
   was created with `ionice` in mind, but we can abuse that placeholder to write
   pids to the `cgroup.procs` file in the portage slice.

   Add the following line to your `/etc/portage/make.conf`:

   ```
   PORTAGE_IONICE_COMMAND="sh -c \"echo \${PID} > /sys/fs/cgroup/systemd/portage.slice/cgroup.procs\""
   ```

   The `cgroup.procs` file is present in every cgroup and contains a list of
   processes that are members of that particular cgroup. Writing a PID to this
   file will move all threads in that process at once to the cgroup. And that,
   is awesome :D