Add a portal to manage cgroups #1152
Replies: 6 comments
-
Proposal for a cgroup portalModern Linux Desktops place each application into its own cgroup using systemd. At this point, the infrastructure for this is well established and doing so has a lot of benefits. From correctly displaying resource consumptions of applications, to improving resource distribution and isolation between applications. However, some applications, in particular browsers, tend to be large. In a lot of ways these are a single applications, but there are advantages to not always consider them as a single item. This has become very apparent with the introduction of systemd-oomd, which will always kill a whole cgroup. In the case of browsers, this currently means that the whole browser will be killed, even if it would be sufficient to kill one of its worker processes. Even better, if we can prefer killing a worker process that corresponds to an inactive tab. RequirementsAs per the above introduction, the following considerations are relevant:
Having a hierarchical layout, where the workers and main application are part of the same cgroup and all workers are sibblings lends itself to this. It does however imply that ProposalThe proposed solution is to manage worker cgroups independently of systemd using an XDG portal. The portal would:
The XDG portal would provide a fixed set of properties, that are attached to the cgroup by setting the xattr user.xdg.. Currently defined is:
See below for a possible API, the proposal has the following known caveats:
AlternativesThe main alternative would be for applications to launch separate systemd units themselves. This method is preferential for e.g. launching long running background tasks into background.slice (e.g. for backup solutions). Ideally applications will create separate DBus services in order to do so. However, this approach will invariable split applications across multiple systemd units and does not allow dynamic resource allocation changes on its own. Another alternative is for applications to manage the cgroup hierarchy themselves. This method is feasible, in particular if sandboxed applications could be permitted direct access to their cgroup hierarchy. This puts the burden on application developers to detect various setups though and providing an XDG portal API might be simpler in the long run.
|
Beta Was this translation helpful? Give feedback.
-
It might be a good idea to come up with an easy-to-use non-sandboxed API, and have some users (even with WIP patches) before writing a portal. If there is an existing API, could you please link to it? If there's already an API, is this portal API the minimum API required to implement the higher-level API? Having API users would help with figuring out whether the API is acceptable and usable. Would also be nice to have references to what other OSes use in similar circumstances. |
Beta Was this translation helpful? Give feedback.
-
@hadess we somewhat have an existing non-sandboxed API for MovePID and Spawn at a session level for whole applications. https://invent.kde.org/-/snippets/1111 has easier to read code examples of that. In terms of examples /within/ applications gnome's VTE is the only example I can think of. Currently that does everything low-level itself and involving a separate slice. |
Beta Was this translation helpful? Give feedback.
-
What applications do you hope to port to that API? It might be useful to first port applications to use a simplified API (which might or might not involve adding said API to GLib/Qt/etc., is that already started?) and then make that API available to sandboxed applications through the portal. (I should add that I say "might be useful" in the British sense of the term, I mean "absolutely required") |
Beta Was this translation helpful? Give feedback.
-
Most likely this will mainly be browsers. There are existing APIs in the form of the kernel APIs obviously. These are already in use by e.g. The main questoin here isn't the API itself. The question here is whether we want to use cgroup delegation to solve this problem. And, possibly whether xattrs as signalling mechanism is a good idea (for which we have prior art in systemd-oomd and in Nishal's work for gnome-shell/uresourced integration). Though, even if we decided for a different approach, this rather simple API could likely stay the same even! |
Beta Was this translation helpful? Give feedback.
-
This is pretty much the only thing of interest when it comes to xdg-desktop-portal. If there are users of that API that need sandboxing, then there's a need. From what I can tell, there's no currently no library that wraps it more generically with being an "app spawn" API, and no applications using that API directly. Did I miss something? |
Beta Was this translation helpful? Give feedback.
-
Implementing a way for large applications to manage multiple worker processes will be beneficial in providing better resource distribution and isolating bad actors.
A portal where one can manage worker processes independently of systemd is one solution to this. It will allow applications to further divide their processes into separate scopes(cgroups) and provide more information about them. The desktop environment (resource control daemons) can then act on this information if desired.
The portal would provide the following features:
Spawn/Move processes into a new/existing cgroup - Spawning a new process in the same namespace as the calling process. This enables terminating individual bad actors instead of terminating the entire application. Moving allows for the grouping of similar helper processes and enables resources control daemons to work effectively using the metadata provided. The end result for both cases is that the process ends up in a separate cgroup.
Permit setting "properties" on each cgroup - the portal can provide a fixed set of properties associated with a cgroup, this can be achieved by setting extended attribute user.xdg.
Building upon these features we can then allow an application to set an active or inactive scope, allowing external daemons to preferentially allocate resources.
The ideas mentioned above are from Benjamin Berg's original proposal, we are currently seeking wider feedback before proceeding to implement it. I will be working on this as a part of my GSoC 2021 project.
Edit 1: To add to "why we want this" there are a lot of advantages of not considering large applications as a single entity.
systemd-oomd for example always kills a whole cgroup. So in case of a browser it will kill the entire browser and not one of its worker processes. Implementing the above can allow us to kill a worker process of an inactive tab and have lesser impact to the experience as compared killing an entire application.
Beta Was this translation helpful? Give feedback.
All reactions