1# GameMode
2**GameMode** is a daemon/lib combo for Linux that allows games to request a set of optimisations be temporarily applied to the host OS.
3
4GameMode was designed primarily as a stop-gap solution to problems with the Intel and AMD CPU powersave or ondemand governors, but is now host to a range of optimisation features and configurations.
5
6Currently GameMode includes support for optimisations including:
7* CPU governor
8* I/O priority
9* Kernel scheduler (`SCHED_ISO`)
10* Screensaver inhibiting
11* GPU performance mode (NVIDIA and AMD), GPU overclocking (NVIDIA)
12* Custom scripts
13
14Issues with GameMode should be reported here in the issues section, and not reported to Feral directly.
15
16---
17## Building and installing [![Build Status](https://travis-ci.org/FeralInteractive/gamemode.svg?branch=master)](https://travis-ci.org/FeralInteractive/gamemode)
18
19*It is preferable to install GameMode with your package manager of choice, if available*. There are Ubuntu (Cosmic), Debian, Solus, AUR, Gentoo, Fedora and openSUSE packages available at the time of writing.
20
21### Install Dependencies
22GameMode depends on `meson` for building and `systemd` for internal communication. This repo contains a `bootstrap.sh` script to allow for quick install to the user bus, but check `meson_options.txt` for custom settings.
23
24#### Ubuntu/Debian (you may also need `dbus-user-session`)
25```bash
26apt install meson libsystemd-dev pkg-config ninja-build git
27```
28#### Arch
29```bash
30pacman -S meson systemd git
31```
32#### Fedora
33```bash
34dnf install meson systemd-devel pkg-config git
35```
36#### Gentoo
37Gentoo has an ebuild which builds a stable release from sources. It will also pull in all the dependencies so you can work on the source code.
38```bash
39emerge --ask games-util/gamemode
40```
41You can also install using the latest sources from git:
42```bash
43ACCEPT_KEYWORDS="**" emerge --ask ~games-util/gamemode-9999
44```
45
46### Build and Install GameMode
47Then clone, build and install a release version of GameMode at 1.3.1:
48
49```bash
50git clone https://github.com/FeralInteractive/gamemode.git
51cd gamemode
52git checkout 1.3.1 # omit to build the master branch
53./bootstrap.sh
54```
55
56To uninstall:
57```bash
58systemctl --user stop gamemoded.service
59cd build/
60ninja uninstall
61```
62
63---
64## Requesting GameMode
65
66For games which integrate GameMode support (see list later on), simply running the game will automatically activate GameMode.
67
68For others, you must manually request GameMode when running the game. This can be done by launching the game through `gamemoderun`:
69```bash
70gamemoderun ./game
71```
72Or edit the Steam launch options:
73```bash
74gamemoderun %command%
75```
76
77Note: for older versions of GameMode (before 1.3) use this string in place of `gamemoderun`:
78```
79LD_PRELOAD="$LD_PRELOAD:/usr/\$LIB/libgamemodeauto.so.0"
80```
81Please note the backslash here in `\$LIB` is required.
82
83---
84## Configuration
85
86The daemon can currently be configured using a `gamemode.ini` file. [gamemode.ini](https://github.com/FeralInteractive/gamemode/blob/master/example/gamemode.ini) is an example of what this file would look like, with explanations for all the variables.
87
88Config files are loaded and merged from the following directories, in order:
891. `/usr/share/gamemode/`
902. `/etc/`
913. `$XDG_CONFIG_HOME` or `$HOME/.config/`
924. `$PWD`
93
94The file parsing uses [inih](https://github.com/benhoyt/inih).
95
96---
97## Features
98
99### Scheduling
100GameMode can leverage support for soft real time mode if the running kernel supports `SCHED_ISO` (not currently supported in upstream kernels), controlled by the `softrealtime` option. This adjusts the scheduling of the game to real time without sacrificing system stability by starving other processes.
101
102GameMode can adjust the nice priority of games to give them a slight IO and CPU priority over other background processes, controlled by the `renice` option. This only works if your user is permitted to adjust priorities within the limits configured by PAM. GameMode can be configured to take care of it by passing `with-pam-group=group` to the build options where `group` is a group your user needs to be part of.
103For more information, see `/etc/security/limits.conf`.
104
105Please take note that some games may actually run seemingly slower with `SCHED_ISO` if the game makes use of busy looping while interacting with the graphic driver. The same may happen if you apply too strong nice values. This effect is called priority inversion: Due to the high priority given to busy loops, there may be too few resources left for the graphics driver. Thus, sane defaults were chosen to not expose this effect on most systems. Part of this default is a heuristic which automatically turns off `SCHED_ISO` if GameMode detects three or less CPU cores. Your experience may change based on using GL threaded optimizations, CPU core binding (taskset), the graphic driver, or different CPU architectures. If you experience bad input latency or inconsistent FPS, try switching these configurations on or off first and report back. `SCHED_ISO` comes with a protection against this effect by falling back to normal scheduling as soon as the `SCHED_ISO` process uses more than 70% avarage across all CPU cores. This default value can be adjusted outside of the scope of GameMode (it's in `/proc/sys/kernel/iso_cpu`). This value also protects against compromising system stability, do not set it to 100% as this would turn the game into a full real time process, thus potentially starving all other OS components from CPU resources.
106
107### IO priority
108GameMode can adjust the I/O priority of games to benefit from reduced lag and latency when a game has to load assets on demand. This is done by default.
109
110### For those with overclocked CPUs
111If you have an AMD CPU and have disabled Cool'n'Quiet, or you have an Intel CPU and have disabled SpeedStep, then GameMode's governor settings will not work, as your CPU is not running with a governor. You are already getting maximum performance.
112
113If you are unsure, `bootstrap.sh` will warn you if your system lacks CPU governor control.
114
115Scripts and other features will still work.
116
117### GPU optimisations
118GameMode is able to automatically apply GPU performance mode changes on AMD and NVIDIA, and overclocking on NVIDIA, when activated. AMD support currently requires the `amdgpu` kernel module, and NVIDIA requires the `coolbits` extension to be enabled in the NVIDIA settings.
119
120It is very much encouraged for users to find out their own overclocking limits manually before venturing into configuring them in GameMode, and activating this feature in GameMode assumes you take responsibility for the effects of said overclocks.
121
122More information can be found in the `example/gamemode.ini` file.
123
124Note that both NVIDIA (GPUBoost) and AMD (Overdrive) devices and drivers already attempt to internally overclock if possible, but it is still common for enthusiasts to want to manually push the upper threshold.
125
126---
127## Apps with GameMode integration
128
129### Games
130The following games are known to integrate GameMode support (meaning they don't require any additional configuration to activate GameMode while running):
131* Rise of the Tomb Raider
132* Total War Saga: Thrones of Britannia
133* Total War: WARHAMMER II
134* DiRT 4
135
136### Others
137Other apps which can integrate with GameMode include:
138* GNOME Shell ([via extension](https://github.com/gicmo/gamemode-extension)) - indicates when GameMode is active in the top panel.
139
140---
141## Developers
142
143The design of GameMode has a clear-cut abstraction between the host daemon and library (`gamemoded` and `libgamemode`), and the client loaders (`libgamemodeauto` and `gamemode_client.h`) that allows for safe use without worrying about whether the daemon is installed or running. This design also means that while the host library currently relies on `systemd` for exchanging messages with the daemon, it's entirely possible to implement other internals that still work with the same clients.
144
145### Components
146**gamemoded** runs in the background, activates game mode on request, refcounts and also checks caller PID lifetime. Run `man gamemoded` for command line options.
147
148**libgamemode** is an internal library used to dispatch requests to the daemon. Note: `libgamemode` should never be linked with directly.
149
150**libgamemodeauto** is a simple dynamic library that automatically requests game mode when loaded. Useful to `LD_PRELOAD` into any game as needed.
151
152**gamemode\_client.h** is as single header lib that lets a game request game mode and handle errors.
153
154### Integration
155Developers can integrate the request directly into an app. Note that none of these client methods force your users to have the daemon installed or running - they will safely no-op if the host is missing.
156
157```C
158// Manually with error checking
159#include "gamemode_client.h"
160
161	if( gamemode_request_start() < 0 ) {
162		fprintf( stderr, "gamemode request failed: %s\n", gamemode_error_string() );
163	}
164
165	/* run game... */
166
167	gamemode_request_end(); // Not required, gamemoded can clean up after game exits
168```
169
170```C
171// Automatically on program start and finish
172#define GAMEMODE_AUTO
173#include "gamemode_client.h"
174```
175
176Or, distribute `libgamemodeauto.so` and either add `-lgamemodeauto` to your linker arguments, or add it to an LD\_PRELOAD in a launch script.
177
178### Supervisor support
179Developers can also create apps that manage GameMode on the system, for other processes:
180
181```C
182#include "gamemode_client.h"
183
184	gamemode_request_start_for(gamePID);
185	gamemode_request_end_for(gamePID);
186```
187
188This functionality can also be controlled in the config file in the `supervisor` section.
189
190---
191## Contributions
192
193### Pull Requests
194Pull requests must match with the coding style found in the `.clang-format` file, please run this before committing:
195```
196clang-format -i $(find . -name '*.[ch]' -not -path "*subprojects/*")
197```
198
199### Planned Features
200* Additional mode-switch plugins
201* Improved client state tracking (PID is unreliable)
202
203### Maintained by
204Feral Interactive
205
206See the [contributors](https://github.com/FeralInteractive/gamemode/graphs/contributors) section for an extended list of contributors.
207
208---
209## License
210
211Copyright © 2017-2019 Feral Interactive
212
213GameMode is available under the terms of the BSD 3-Clause License (Revised)
214