[platformx/x11] Add a freeze protection against OpenGL

Summary:
With nouveau driver it can happen that KWin gets frozen when first trying
to render with OpenGL. This results in a freeze of the complete desktop
as the compositor is non functional.

Our OpenGL breakage detection is only able to detect crashes, but not
freezes. This change improves it by also added a freeze protection.

In the PreInit stage a thread is started with a QTimer of 15 sec. If the
timer fires, qFatal is triggered to terminate KWin. This can only happen
if the creation of the OpenGL compositor takes longer than said 15 sec.

In the PostInit stage the timer gets deleted and the thread stopeed
again.

Thus if a freeze is detected the OpenGL unsafe protection is written into
the config. KWin aborts and gets restarted by DrKonqui. The new KWin
instance will no longer try to activate the freezing OpenGL as the
protection is set.

If KWin doesn't freeze the protection is removed from the config as
we are used to.

Check for freezes for the first n frames, not just the first

This patch changes the freeze detection code to detect freezes in the
first 30 frames (by default, users can change that with the
KWIN_MAX_FRAMES_TESTED environment variable). This detects
successfully the freezes associated to nouveau drivers
in https://bugzilla.suse.com/show_bug.cgi?id=1005323

Reviewers: davidedmundson, #plasma, #kwin, graesslin

Reviewed By: #plasma, #kwin, graesslin

Subscribers: luebking, graesslin, kwin, plasma-devel, davidedmundson

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D3132
This commit is contained in:
Antonio Larrosa 2016-10-24 17:14:32 +02:00
parent 2e9dc7f28a
commit 19147f5f85
5 changed files with 60 additions and 3 deletions

View file

@ -135,6 +135,9 @@ Compositor::Compositor(QObject* workspace)
} }
); );
if (qEnvironmentVariableIsSet("KWIN_MAX_FRAMES_TESTED"))
m_framesToTestForSafety = qEnvironmentVariableIntValue("KWIN_MAX_FRAMES_TESTED");
// register DBus // register DBus
new CompositorDBusInterface(this); new CompositorDBusInterface(this);
} }
@ -207,7 +210,6 @@ void Compositor::slotCompositingOptionsInitialized()
m_scene = SceneOpenGL::createScene(this); m_scene = SceneOpenGL::createScene(this);
// TODO: Add 30 second delay to protect against screen freezes as well
kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PostInit); kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PostInit);
if (m_scene && !m_scene->initFailed()) { if (m_scene && !m_scene->initFailed()) {
@ -734,7 +736,19 @@ void Compositor::performCompositing()
// clear all repaints, so that post-pass can add repaints for the next repaint // clear all repaints, so that post-pass can add repaints for the next repaint
repaints_region = QRegion(); repaints_region = QRegion();
if (m_framesToTestForSafety > 0 && (m_scene->compositingType() & OpenGLCompositing)) {
kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PreFrame);
}
m_timeSinceLastVBlank = m_scene->paint(repaints, windows); m_timeSinceLastVBlank = m_scene->paint(repaints, windows);
if (m_framesToTestForSafety > 0) {
if (m_scene->compositingType() & OpenGLCompositing) {
kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PostFrame);
}
m_framesToTestForSafety--;
if (m_framesToTestForSafety == 0 && (m_scene->compositingType() & OpenGLCompositing)) {
kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PostLastGuardedFrame);
}
}
m_timeSinceStart += m_timeSinceLastVBlank; m_timeSinceStart += m_timeSinceLastVBlank;
if (waylandServer()) { if (waylandServer()) {

View file

@ -239,6 +239,7 @@ private:
Scene *m_scene; Scene *m_scene;
bool m_bufferSwapPending; bool m_bufferSwapPending;
bool m_composeAtSwapCompletion; bool m_composeAtSwapCompletion;
int m_framesToTestForSafety = 3;
KWIN_SINGLETON_VARIABLE(Compositor, s_compositor) KWIN_SINGLETON_VARIABLE(Compositor, s_compositor)
}; };

View file

@ -142,7 +142,10 @@ public:
virtual bool openGLCompositingIsBroken() const; virtual bool openGLCompositingIsBroken() const;
enum class OpenGLSafePoint { enum class OpenGLSafePoint {
PreInit, PreInit,
PostInit PostInit,
PreFrame,
PostFrame,
PostLastGuardedFrame
}; };
/** /**
* This method is invoked before and after creating the OpenGL rendering Scene. * This method is invoked before and after creating the OpenGL rendering Scene.

View file

@ -37,6 +37,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <KConfigGroup> #include <KConfigGroup>
#include <KLocalizedString> #include <KLocalizedString>
#include <QThread>
#include <QOpenGLContext> #include <QOpenGLContext>
#include <QX11Info> #include <QX11Info>
@ -200,12 +201,48 @@ void X11StandalonePlatform::createOpenGLSafePoint(OpenGLSafePoint safePoint)
switch (safePoint) { switch (safePoint) {
case OpenGLSafePoint::PreInit: case OpenGLSafePoint::PreInit:
group.writeEntry(unsafeKey, true); group.writeEntry(unsafeKey, true);
group.sync();
// Deliberately continue with PreFrame
case OpenGLSafePoint::PreFrame:
if (m_openGLFreezeProtectionThread == nullptr) {
Q_ASSERT(m_openGLFreezeProtection == nullptr);
m_openGLFreezeProtectionThread = new QThread(this);
m_openGLFreezeProtectionThread->setObjectName("FreezeDetector");
m_openGLFreezeProtectionThread->start();
m_openGLFreezeProtection = new QTimer;
m_openGLFreezeProtection->setInterval(15000);
m_openGLFreezeProtection->setSingleShot(true);
m_openGLFreezeProtection->start();
m_openGLFreezeProtection->moveToThread(m_openGLFreezeProtectionThread);
connect(m_openGLFreezeProtection, &QTimer::timeout, m_openGLFreezeProtection,
[] {
const QString unsafeKey(QLatin1String("OpenGLIsUnsafe") + (kwinApp()->isX11MultiHead() ? QString::number(kwinApp()->x11ScreenNumber()) : QString()));
auto group = KConfigGroup(kwinApp()->config(), "Compositing");
group.writeEntry(unsafeKey, true);
group.sync();
qFatal("Freeze in OpenGL initialization detected");
}, Qt::DirectConnection);
} else {
Q_ASSERT(m_openGLFreezeProtection);
QMetaObject::invokeMethod(m_openGLFreezeProtection, "start", Qt::QueuedConnection);
}
break; break;
case OpenGLSafePoint::PostInit: case OpenGLSafePoint::PostInit:
group.writeEntry(unsafeKey, false); group.writeEntry(unsafeKey, false);
group.sync();
// Deliberately continue with PostFrame
case OpenGLSafePoint::PostFrame:
QMetaObject::invokeMethod(m_openGLFreezeProtection, "stop", Qt::QueuedConnection);
break;
case OpenGLSafePoint::PostLastGuardedFrame:
m_openGLFreezeProtection->deleteLater();
m_openGLFreezeProtection = nullptr;
m_openGLFreezeProtectionThread->quit();
m_openGLFreezeProtectionThread->wait();
delete m_openGLFreezeProtectionThread;
m_openGLFreezeProtectionThread = nullptr;
break; break;
} }
group.sync();
} }
} }

View file

@ -62,6 +62,8 @@ private:
static bool hasGlx(); static bool hasGlx();
XInputIntegration *m_xinputIntegration = nullptr; XInputIntegration *m_xinputIntegration = nullptr;
QThread *m_openGLFreezeProtectionThread = nullptr;
QTimer *m_openGLFreezeProtection = nullptr;
}; };