Skip to content

High xfsettings CPU usage upon connecting external keyboard; patch attached

Upon resuming from system sleep or attaching an external keyboard, xfsettingsd exhibits high CPU usage for over a minute, during which time no keyboard shortcuts are usable. Probing with xscope exposes an avalanche of XGrabKey requests to the X server, with various key codes. Running xfsettingsd in debug mode reveals an anomaly – attaching an external keyboard results in several events to that effect:

New keyboard detected; restoring XKB settings.

Apparently developers are aware of this issue: keyboard-layout.c:xfce_keyboard_layout_reset_xkl_config contains the comment:

We get X-new-device notifications multiple times for a single keyboard device (why?)

It seems that (the unmaintained) libxklavier bombards xfsettingsd with several events per a single physical keyboard attaching event and subsequently some expensive operations are also run many times (processing ~/.Xmodmap looks to be the culprit, or one of the culprits).

I am attaching a patch for a workaround which throttles invoking xfce_keyboard_layout_reset_xkl_config to at most once per 2 seconds. This resolves the issue for me with the duration of 100% usage of a single CPU core + keyboard shortcuts being unavailable reduced from >60s to ~8s.

This could possibly also resolve #167 (closed), #168 (closed), #116 (closed) and #89 (closed) and others.

xfsettingsd-throttle-add-kb.patch

Edited by tambeta