Ikey Doherty 68e326de60 Transform into a full D-BUS service with Polkit support
Primarily we convert the service into a thread safe one that isn't reliant
on signaling for control flow, eliminating data race conditions. We also
enable interleaving by separating game mode pivoting from explicit client
registration.

The static pid list is now converted into a dynamic list that is OOM safe
to store all registered clients (with a reasonable upper limit of 256 clients)
to better handle cases where LD_PRELOAD is used for a large process group.
Additionally we begin storing some metadata on the connected clients such
as their executable path, which will enable us to perform some basic
whitelisting in future.

The cpugovctl binary is now moved into the libexecdir as an explicit helper
of the D-BUS service, using the shared library to merge some code back into
the daemon. This saves having to execute a process to query the state of the
governors, as we don't need a privileged client to do this.

In order to sanely set the governors, we require that the binary is running
as euid 0, and execute this using `pkexec`. A PolKit policy definition is
provided which allows active/logged in users to execute this helper through
a path whitelist. As such we can convert the daemon into user-mode only, with
the privileged helper being dispatched exclusively via polkit. This removes
the need for a setuid helper or having a system mode daemon.

Lastly we clean up the codebase a bit to be consistent with modern C code
conventions, using pragmas where available. The library component still uses
the older ifdef approach to support older compilers, but the daemon portion
uses the directive to simplify intent and speed up compilation. Additionally
we move all comments to C style comments for consistency, instead of mixing
in C++ style single line comments, in order to establish a formal coding
style.

The net result is a more robust service which can be D-BUS activated when
clients need it, that can perform scaling automatically without harassing
the user with authentication popups.

Signed-off-by: Ikey Doherty <ikey@solus-project.com>
2018-03-05 17:32:25 +00:00
2018-03-05 17:32:01 +00:00
2018-03-05 17:32:01 +00:00
2018-03-05 17:32:01 +00:00
2018-03-05 17:32:01 +00:00

GameMode

A preliminary implementation of a daemon/lib combo to allow games to request a performance mode from the host OS on Linux. It was designed primarily as a stop-gap solution to problems with the Intel and AMD CPU powersave or ondemand governors, but is intended to be expanded beyond just CPU power states as needed.

Currently using sd-bus on the user bus internally for messaging


Components

Host

gamemoded

Runs in the background, waits for requests, refcounts and also checks caller PID lifetime.

Accepts -d (daemonize) and -l (log to syslog)

libgamemode

Dynamic library to dispatch requests to the daemon

Note: Behaviour of gamemoded may change, so libgamemode should never be linked with directly.

cpugovctl

Small program used to to control the cpu governor.

Accepts get (gets current governor) and set <GOVERNOR> (sets the current governor).

Clients

libgamemodeauto

Simple dynamic library that automatically requests game mode when loaded. Minimal dependencies.

Useful to LD_PRELOAD into any game as needed.

gamemode_client.h

Very small header only lib that lets a game request game mode and handle errors. Minimal dependencies.

Can also be included with GAMEMODE_AUTO defined to behave automatically.


Build and install

Daemon and host library

Dependencies

  • meson
  • systemd
# Ubuntu
apt install meson libsystemd-dev
# Arch
pacman -S meson systemd
git clone <git repo>
cd gamemode
meson --prefix=/usr build
cd build
ninja
sudo ninja install

Using with any game or program

After installing libgamemodeauto.so simple preload it into the program. Examples:

LD_PRELOAD=/usr/\$LIB/libgamemodeauto.so ./game

Or steam launch options

LD_PRELOAD=/usr/\$LIB/libgamemodeauto.so %command%

Building into a game or program

You may want to build in functionality directly into an app, this stops the need for users to have to manually set anything up.

gamemode_client.h and libgamemodeauto are safe to use regardless of whether libgamemode and gamemoded are installed on the system. They also do not require systemd.

Using directly

#include "gamemode_client.h"

	if( gamemode_request_start() < 0 )
		fprintf( stderr, "gamemode request failed: %s\n", gamemode_error_string() );

	/* run game... */

	gamemode_request_end(); // Not required, gamemoded will clean up after game exists anyway

Using automatically

Option 1: Include in code

#define GAMEMODE_AUTO
#include "gamemode_client.h"

Add -lgamemodeauto to linker arguments and distribute libgamemodeauto.so with the game

Option 3: Distribute and script

Distribute libgamemodeauto.so with the game and add to LD_PRELOAD in a launch script


Pull Requests

Pull requests must match with the coding style found in the .clang-format file

clang-format -i $(find . -name '*.[ch]')

TODO

  • Use polkit for cpugovctl (currently simply using chmod +4555)
  • Implement some kind of user confuguration to allow for whitelists, extra behaviour, etc.
Description
Optimise Linux system performance on demand
Readme 1.4 MiB
Languages
C 90%
Meson 5.5%
Roff 2.9%
Shell 1.6%