182.4. switcheroo-control-3.0
D-Bus service that tells launchers which GPU is discrete, so an application can be started on it
| Version | 3.0, release 1 |
| Shelf | hardware — Device access, bus and sensor tools, power management, and Bluetooth |
| Phase | 42_graphics (Graphics Stack), 130 of 189 in build order |
| Licence | GPL-3.0-or-later |
| Homepage | https://gitlab.freedesktop.org/hadess/switcheroo-control |
| Build time | 0.1 SBU |
| Installed size | 149.9 KiB in 10 files |
| Recipe | ports/core/hardware/switcheroo-control/kpkgbuild |
About
switcheroo-control is a small system-bus service, net.hadess.SwitcherooControl, that tells a launcher which GPUs a machine has, which of them is discrete, and which environment variables start a program on each. KIO’s “run on the discrete GPU” and switcherooctl launch read it. Its udev rule tags each discrete GPU, running a small probe for amdgpu, nouveau and xe cards that asks the kernel driver through libdrm, and its hardware-database file names Intel’s GPUs. The probes are why the port is built in 42_graphics rather than with the other hardware services in 41_system.
Why it is in KDOS
The comment above it in script/phases/42_graphics/packages.d/hardware.txt:
Which GPU is discrete, for a launcher's "run on the dedicated GPU". Here and not in 41_system because it probes amdgpu, nouveau and xe through libdrm.
Sources
switcheroo-control-3.0.tar.xz(32.6 KiB)
sha256dddcb43c58acb334b529387da8f3abfee57346099dce8f438a167f5ebc34a9da
upstream: https://gitlab.freedesktop.org/-/project/4339/uploads/14da8baf1bd4ab2b767a56f3921470f1/switcheroo-control-3.0.tar.xz
archive copy: sources-3/hardware--switcheroo-control-3.0.tar.xz
Dependencies
Depends on: meson, ninja, pkgconf, glib, libgudev, libdrm, eudev, python3, python3-gobject.
Required by (1): kio.
Installation
From the repository root, inside the build container, this is the line kdosbuild runs for the port:
script/chroot/exec.sh bash -c 'source script/phases/42_graphics/phase.env && kpkg install switcheroo-control'
What kpkg does
kpkg runs these steps, in this order, as root inside the chroot. The port directory is /ports/core/hardware/switcheroo-control.
1. Verify every declared source. Each file a sha256 = line names, found in the port directory or in /var/cache/kpkg/sources, must hash to its line; a mismatch stops the build before anything is unpacked.
cd /ports/core/hardware/switcheroo-control
echo 'dddcb43c58acb334b529387da8f3abfee57346099dce8f438a167f5ebc34a9da switcheroo-control-3.0.tar.xz' | sha256sum -c -
2. Unpack. The work directory is emptied and the first source is unpacked into $SRC with its top directory stripped; any later tarball lands unstripped beside it in $SRC_ROOT, and a file that is not a tarball is copied into $SRC.
SRC_ROOT=/var/cache/kpkg/work/switcheroo-control
SRC=$SRC_ROOT/switcheroo-control-3.0
PKG=$SRC_ROOT/pkg
rm -rf "$SRC_ROOT"
mkdir -p "$SRC" "$PKG"
tar --no-same-permissions --warning=no-unknown-keyword -xf switcheroo-control-3.0.tar.xz -C "$SRC" --strip-components=1
Every config.guess and config.sub older than automake’s is then replaced with automake’s, so a configure script recognises the musl target.
3. Build. build.sh is sourced in a subshell under set -e, from $SRC, with name, version, release and the recipe’s helpers _up and the five paths PKGNAME, PORT_SRC, SRC_ROOT, SRC and PKG set as shell variables. Its body, verbatim:
meson setup build \
--prefix=/usr \
--buildtype=release \
-Dsystemdsystemunitdir=/usr/lib/systemd/system \
-Dhwdbdir=/usr/lib/udev/hwdb.d \
-Drulesdir=/usr/lib/udev/rules.d \
-Dlibdrm=enabled \
-Dlibdrm_amdgpu=enabled \
-Dlibdrm_nouveau=enabled \
-Ddrm_xe=enabled \
-Dgtk_doc=false \
-Dtests=false
meson compile -C build
DESTDIR=$PKG meson install --no-rebuild -C build
rm -rf "$PKG/usr/lib/systemd"
install -d "$PKG/etc/init.d"
cat > "$PKG/etc/init.d/44_switcheroo-control.sh" <<'KDOS_SH'
#!/bin/bash
. /etc/init.d/service_helper
NAME="switcheroo-control"
DAEMON="/usr/libexec/switcheroo-control"
case "$1" in
start)
[ ! -x "$DAEMON" ] && { echo "[SKIP] $NAME: $DAEMON not found"; exit 0; }
gpus=0
for c in /sys/class/drm/card[0-9]*; do
case "${c##*/}" in *-*) continue ;; esac
[ -e "$c/device" ] && gpus=$((gpus + 1))
done
if [ "$gpus" -lt 2 ]; then
echo "[SKIP] $NAME: fewer than two GPUs"
exit 0
fi
echo "[KDOS] Starting $NAME..."
supervise "$NAME" "$DAEMON"
;;
stop) stop_service "$NAME" ;;
status) check_status "$NAME" ;;
*) echo "Usage: $0 {start|stop|status}"; exit 1 ;;
esac
KDOS_SH
chmod 755 "$PKG/etc/init.d/44_switcheroo-control.sh"
The switches, and why each is set:
-Dsystemdsystemunitdir=/usr/lib/systemd/system— Where upstream installs its systemd unit. Left empty, meson askssystemd.pcfor the directory and stops, since KDOS has no systemd (see No systemd); the unit is installed into the named directory andbuild.shdeletes it from the package.-Dhwdbdir=/usr/lib/udev/hwdb.d,-Drulesdir=/usr/lib/udev/rules.d— Where the hardware-database file (30-pci-intel-gpu.hwdb) and the rule (30-discrete-gpu.rules) go, named directly rather than read fromudev.pc.-Dlibdrm=enabled,-Dlibdrm_amdgpu=enabled,-Dlibdrm_nouveau=enabled,-Ddrm_xe=enabled— The three probe programs,switcheroo-control-check-discrete-amdgpu,-nouveauand-xe. Each isautoupstream and is built only when its library or header is found;enabledmakes each a requirement.-Dgtk_doc=false,-Dtests=false— No API documentation and no test run, upstream’s defaults.
The common switches of each build system are explained in the Flag glossary.
4. Package. kpkg removes libtool archives, the info directory index, mimeinfo.cache and every fonts.dir from $PKG, copies the source’s licence files into /usr/share/licenses/switcheroo-control, prunes what the image does not ship, lints the tree and rolls it reproducibly:
tar --sort=name --format=gnu --numeric-owner --owner=0 --group=0 \
--mtime=@$SOURCE_DATE_EPOCH \
--use-compress-program='xz -9 -T0 --block-size=32MiB --no-adjust' \
-cf switcheroo-control-3.0-1.tar.xz -C "$PKG" .
5. Adopt. kpkgadd installs the package’s files over the root, refusing a path another package owns, records the file list in /var/lib/kpkg/db/switcheroo-control, and runs the triggers its paths call for.
Contents
Installed programs: switcherooctl.
Installed directories: /usr/share/licenses/switcheroo-control.
Notes
/etc/init.d/44_switcheroo-control.sh starts the daemon under the supervisor only on a machine with two or more GPUs: it counts the card* entries under /sys/class/drm that have a device, skipping the connector entries, whose names contain a dash. With one GPU there is nothing to choose between, and the service skips (see Services that skip themselves).