XWayland: Don't dispatch xwayland events in QAbstractEventDispatcher sleeps

QAbstractEventDispatcher blocks and waits constantly on every external event
processed; every timer or update from an X or wayland client, mouse
move or DRM event.

Right now every time this happens we go and check Xwayland for new
events, this is a system call (poll) that based on strace will
unsurprisingly immediately return with EAGAIN as there's nothing to read
from X. If there is something to read our socket notifier will fire. On block we do still need to read any events read in the meantime that weren't dispatched.

This cuts down our system calls significantly, which hopefully should have a
noticeable impact on performance especially when the kernel is under
load.

---

Found whilst analysing strace (by accident whilst looking for something else!)

In a simple case of xwayland nested running glxgears we go from 28 calls per frame to 21. With many many clients and more input events it'll be an even higher percentage.

(cherry picked from commit fbb71f9c0bcc28e26f096556f9e6da0c299b4fd4)
icc-effect-5.27.2
David Edmundson 2023-02-08 10:34:20 +00:00 committed by Vlad Zahorodnii
parent 98d1033c82
commit fdd37ec6f1
2 changed files with 21 additions and 8 deletions

View File

@ -217,7 +217,7 @@ XwaylandLauncher *Xwayland::xwaylandLauncher() const
return m_launcher;
}
void Xwayland::dispatchEvents()
void Xwayland::dispatchEvents(DispatchEventsMode mode)
{
xcb_connection_t *connection = kwinApp()->x11Connection();
if (!connection) {
@ -232,12 +232,15 @@ void Xwayland::dispatchEvents()
return;
}
while (xcb_generic_event_t *event = xcb_poll_for_event(connection)) {
auto pollEventFunc = mode == DispatchEventsMode::Poll ? xcb_poll_for_event : xcb_poll_for_queued_event;
while (xcb_generic_event_t *event = pollEventFunc(connection)) {
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
long result = 0;
#else
qintptr result = 0;
#endif
QAbstractEventDispatcher *dispatcher = QCoreApplication::eventDispatcher();
dispatcher->filterNativeEvent(QByteArrayLiteral("xcb_generic_event_t"), event, &result);
free(event);
@ -251,18 +254,23 @@ void Xwayland::installSocketNotifier()
const int fileDescriptor = xcb_get_file_descriptor(kwinApp()->x11Connection());
m_socketNotifier = new QSocketNotifier(fileDescriptor, QSocketNotifier::Read, this);
connect(m_socketNotifier, &QSocketNotifier::activated, this, &Xwayland::dispatchEvents);
connect(m_socketNotifier, &QSocketNotifier::activated, this, [this]() {
dispatchEvents(DispatchEventsMode::Poll);
});
QAbstractEventDispatcher *dispatcher = QCoreApplication::eventDispatcher();
connect(dispatcher, &QAbstractEventDispatcher::aboutToBlock, this, &Xwayland::dispatchEvents);
connect(dispatcher, &QAbstractEventDispatcher::awake, this, &Xwayland::dispatchEvents);
connect(dispatcher, &QAbstractEventDispatcher::aboutToBlock, this, [this]() {
dispatchEvents(DispatchEventsMode::EventQueue);
});
connect(dispatcher, &QAbstractEventDispatcher::awake, this, [this]() {
dispatchEvents(DispatchEventsMode::EventQueue);
});
}
void Xwayland::uninstallSocketNotifier()
{
QAbstractEventDispatcher *dispatcher = QCoreApplication::eventDispatcher();
disconnect(dispatcher, &QAbstractEventDispatcher::aboutToBlock, this, &Xwayland::dispatchEvents);
disconnect(dispatcher, &QAbstractEventDispatcher::awake, this, &Xwayland::dispatchEvents);
disconnect(dispatcher, nullptr, this, nullptr);
delete m_socketNotifier;
m_socketNotifier = nullptr;

View File

@ -57,7 +57,6 @@ Q_SIGNALS:
private Q_SLOTS:
void handleXwaylandFinished();
void handleXwaylandReady();
void dispatchEvents();
void handleSelectionLostOwnership();
void handleSelectionFailedToClaimOwnership();
@ -66,6 +65,12 @@ private Q_SLOTS:
private:
friend class XrandrEventFilter;
enum class DispatchEventsMode {
Poll,
EventQueue
};
void dispatchEvents(DispatchEventsMode mode);
void installSocketNotifier();
void uninstallSocketNotifier();
void updatePrimary();