Commit 2d27c553 authored by Nikolai Kosjar's avatar Nikolai Kosjar

Clang: Suspend least recently used translation units

...to free some memory.

The translation units of the 7 most recently used documents ("hot
documents", tracked by document visibility) are kept in memory.
Translation units of other documents are suspended and will be resumed
once they become visible again.

The resumption of a translation unit needs the same time as reparse
(since it is a reparse effectively).

The number of hot documents can be modified by the run time environment
variable QTC_CLANG_HOT_DOCUMENTS=N. Visible documents are always hot.

Task-number: QTCREATORBUG-11640
Change-Id: I68ecd2b1373e303372300203e42d90f65a4b39b3
Reviewed-by: Ivan Donchevskii's avatarIvan Donchevskii <ivan.donchevskii@qt.io>
Reviewed-by: Marco Bubke's avatarMarco Bubke <marco.bubke@qt.io>
parent c2cfc62f
......@@ -25,6 +25,8 @@
#pragma once
#include <clang-c/Index.h>
namespace ClangBackEnd {
enum class PreferredTranslationUnit
......@@ -34,4 +36,9 @@ enum class PreferredTranslationUnit
LastUninitialized,
};
// CLANG-UPGRADE-CHECK: Remove IS_SUSPEND_SUPPORTED once we require clang >= 5.0
#if defined(CINDEX_VERSION_HAS_BACKPORTED_SUSPEND) || CINDEX_VERSION_MINOR >= 41
# define IS_SUSPEND_SUPPORTED
#endif
} // namespace ClangBackEnd
......@@ -12,6 +12,7 @@ HEADERS += \
$$PWD/clangdocumentprocessor.h \
$$PWD/clangdocumentprocessors.h \
$$PWD/clangdocuments.h \
$$PWD/clangdocumentsuspenderresumer.h \
$$PWD/clangexceptions.h \
$$PWD/clangfilepath.h \
$$PWD/clangfilesystemwatcher.h \
......@@ -25,8 +26,10 @@ HEADERS += \
$$PWD/clangreparsesupportivetranslationunitjob.h \
$$PWD/clangrequestdocumentannotationsjob.h \
$$PWD/clangrequestreferencesjob.h \
$$PWD/clangresumedocumentjob.h \
$$PWD/clangstring.h \
$$PWD/clangsupportivetranslationunitinitializer.h \
$$PWD/clangsuspenddocumentjob.h \
$$PWD/clangtranslationunit.h \
$$PWD/clangtranslationunits.h \
$$PWD/clangtranslationunitupdater.h \
......@@ -63,6 +66,7 @@ SOURCES += \
$$PWD/clangdocumentprocessor.cpp \
$$PWD/clangdocumentprocessors.cpp \
$$PWD/clangdocuments.cpp \
$$PWD/clangdocumentsuspenderresumer.cpp \
$$PWD/clangexceptions.cpp \
$$PWD/clangfilepath.cpp \
$$PWD/clangfilesystemwatcher.cpp \
......@@ -72,10 +76,12 @@ SOURCES += \
$$PWD/clangjobrequest.cpp \
$$PWD/clangjobs.cpp \
$$PWD/clangparsesupportivetranslationunitjob.cpp \
$$PWD/clangresumedocumentjob.cpp \
$$PWD/clangreferencescollector.cpp \
$$PWD/clangreparsesupportivetranslationunitjob.cpp \
$$PWD/clangrequestdocumentannotationsjob.cpp \
$$PWD/clangrequestreferencesjob.cpp \
$$PWD/clangsuspenddocumentjob.cpp \
$$PWD/clangsupportivetranslationunitinitializer.cpp \
$$PWD/clangtranslationunit.cpp \
$$PWD/clangtranslationunits.cpp \
......
......@@ -26,6 +26,7 @@
#include "clangcodemodelserver.h"
#include "clangdocuments.h"
#include "clangdocumentsuspenderresumer.h"
#include "clangfilesystemwatcher.h"
#include "clangtranslationunits.h"
#include "codecompleter.h"
......@@ -89,6 +90,7 @@ void ClangCodeModelServer::registerTranslationUnitsForEditor(const ClangBackEnd:
unsavedFiles.createOrUpdate(message.fileContainers());
documents.setUsedByCurrentEditor(message.currentEditorFilePath());
documents.setVisibleInEditors(message.visibleEditorFilePaths());
processSuspendResumeJobs(documents.documents());
processInitialJobsForDocuments(createdDocuments);
} catch (const std::exception &exception) {
......@@ -264,6 +266,8 @@ void ClangCodeModelServer::updateVisibleTranslationUnits(const UpdateVisibleTran
try {
documents.setUsedByCurrentEditor(message.currentEditorFilePath());
documents.setVisibleInEditors(message.visibleEditorFilePaths());
processSuspendResumeJobs(documents.documents());
updateDocumentAnnotationsTimer.start(0);
} catch (const std::exception &exception) {
qWarning() << "Error in ClangCodeModelServer::updateVisibleTranslationUnits:" << exception.what();
......@@ -347,6 +351,16 @@ void ClangCodeModelServer::processJobsForDirtyAndVisibleButNotCurrentDocuments()
addAndRunUpdateJobs(documents.dirtyAndVisibleButNotCurrentDocuments());
}
void ClangCodeModelServer::processSuspendResumeJobs(const std::vector<Document> &documents)
{
const SuspendResumeJobs suspendResumeJobs = createSuspendResumeJobs(documents);
for (const SuspendResumeJobsEntry &entry : suspendResumeJobs) {
DocumentProcessor processor = documentProcessors().processor(entry.document);
processor.addJob(entry.jobRequestType, entry.preferredTranslationUnit);
processor.process();
}
}
void ClangCodeModelServer::processInitialJobsForDocuments(const std::vector<Document> &documents)
{
for (const auto &document : documents) {
......
......@@ -77,6 +77,7 @@ private:
void processJobsForDirtyCurrentDocument();
void processTimerForVisibleButNotCurrentDocuments();
void processJobsForDirtyAndVisibleButNotCurrentDocuments();
void processSuspendResumeJobs(const std::vector<Document> &documents);
void addAndRunUpdateJobs(std::vector<Document> documents);
......
......@@ -80,6 +80,7 @@ public:
bool isUsedByCurrentEditor = false;
bool isVisibleInEditor = false;
bool increaseResponsiveness = false;
bool isSuspended = false;
};
DocumentData::DocumentData(const Utf8String &filePath,
......@@ -226,6 +227,20 @@ void Document::setResponsivenessIncreaseNeeded(bool responsivenessIncreaseNeeded
d->increaseResponsiveness = responsivenessIncreaseNeeded;
}
bool Document::isSuspended() const
{
checkIfNull();
return d->isSuspended;
}
void Document::setIsSuspended(bool isSuspended)
{
checkIfNull();
d->isSuspended = isSuspended;
}
bool Document::isUsedByCurrentEditor() const
{
checkIfNull();
......
......@@ -93,6 +93,9 @@ public:
bool isResponsivenessIncreaseNeeded() const;
void setResponsivenessIncreaseNeeded(bool responsivenessIncreaseNeeded);
bool isSuspended() const;
void setIsSuspended(bool isSuspended);
bool isUsedByCurrentEditor() const;
void setIsUsedByCurrentEditor(bool isUsedByCurrentEditor);
......
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "clangdocumentsuspenderresumer.h"
#include "clangbackendipc_global.h"
#include "clangdocumentprocessors.h"
#include "clangdocuments.h"
#include <utils/algorithm.h>
#include <algorithm>
namespace ClangBackEnd {
constexpr int DefaultHotDocumentsSize = 7;
void categorizeHotColdDocuments(int hotDocumentsSize,
const std::vector<Document> &inDocuments,
std::vector<Document> &hotDocuments,
std::vector<Document> &coldDocuments)
{
// Sort documents, most recently used/visible at top
std::vector<Document> documents = inDocuments;
std::stable_sort(documents.begin(), documents.end(), [](const Document &a, const Document &b) {
return a.visibleTimePoint() > b.visibleTimePoint();
});
// Ensure that visible documents are always hot, otherwise not all visible
// documents will be resumed.
const auto isVisible = [](const Document &document) { return document.isVisibleInEditor(); };
const int visibleDocumentsSize = Utils::count(documents, isVisible);
hotDocumentsSize = std::max(hotDocumentsSize, visibleDocumentsSize);
if (documents.size() <= uint(hotDocumentsSize)) {
hotDocuments = documents;
coldDocuments.clear();
} else {
const auto firstColdIterator = documents.begin() + hotDocumentsSize;
hotDocuments = std::vector<Document>(documents.begin(), firstColdIterator);
coldDocuments = std::vector<Document>(firstColdIterator, documents.end());
}
}
#ifdef IS_SUSPEND_SUPPORTED
static int hotDocumentsSize()
{
static int hotDocuments = -1;
if (hotDocuments == -1) {
bool ok = false;
const int fromEnvironment = qEnvironmentVariableIntValue("QTC_CLANG_HOT_DOCUMENTS", &ok);
hotDocuments = ok && fromEnvironment >= 1 ? fromEnvironment : DefaultHotDocumentsSize;
}
return hotDocuments;
}
static SuspendResumeJobs createJobs(const Document &document, JobRequest::Type type)
{
SuspendResumeJobs jobs;
jobs.append({document, type, PreferredTranslationUnit::RecentlyParsed});
if (document.isResponsivenessIncreased())
jobs.append({document, type, PreferredTranslationUnit::PreviouslyParsed});
return jobs;
}
static bool isFineDocument(const Document &document)
{
return !document.isNull() && document.isIntact();
}
static bool isSuspendable(const Document &document)
{
return isFineDocument(document)
&& !document.isSuspended()
&& !document.isVisibleInEditor();
}
static bool isResumable(const Document &document)
{
return isFineDocument(document)
&& document.isSuspended()
&& document.isVisibleInEditor();
}
#endif // IS_SUSPEND_SUPPORTED
SuspendResumeJobs createSuspendResumeJobs(const std::vector<Document> &documents,
int customHotDocumentSize)
{
Q_UNUSED(documents);
Q_UNUSED(customHotDocumentSize);
SuspendResumeJobs jobs;
#ifdef IS_SUSPEND_SUPPORTED
std::vector<Document> hotDocuments;
std::vector<Document> coldDocuments;
const int size = (customHotDocumentSize == -1) ? hotDocumentsSize() : customHotDocumentSize;
categorizeHotColdDocuments(size, documents, hotDocuments, coldDocuments);
// Cold documents should be suspended...
const std::vector<Document> toSuspend = Utils::filtered(coldDocuments, &isSuspendable);
for (const Document &document : toSuspend)
jobs += createJobs(document, JobRequest::Type::SuspendDocument);
// ...and hot documents that were suspended should be resumed
const std::vector<Document> toResume = Utils::filtered(hotDocuments, &isResumable);
for (const Document &document : toResume)
jobs += createJobs(document, JobRequest::Type::ResumeDocument);
#endif // IS_SUSPEND_SUPPORTED
return jobs;
}
} // namespace ClangBackEnd
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "clangdocument.h"
#include "clangjobrequest.h"
#include <vector>
namespace ClangBackEnd {
class SuspendResumeJobsEntry {
public:
SuspendResumeJobsEntry() = default;
SuspendResumeJobsEntry(const Document &document,
JobRequest::Type jobRequestType,
PreferredTranslationUnit preferredTranslationUnit)
: document(document)
, jobRequestType(jobRequestType)
, preferredTranslationUnit(preferredTranslationUnit)
{
}
Document document;
JobRequest::Type jobRequestType = JobRequest::Type::SuspendDocument;
PreferredTranslationUnit preferredTranslationUnit = PreferredTranslationUnit::RecentlyParsed;
};
using SuspendResumeJobs = QVector<SuspendResumeJobsEntry>;
SuspendResumeJobs createSuspendResumeJobs(const std::vector<Document> &documents,
int customHotDocumentCounts = -1);
// for tests
void categorizeHotColdDocuments(int hotDocumentsSize,
const std::vector<Document> &inDocuments,
std::vector<Document> &hotDocuments,
std::vector<Document> &coldDocuments);
} // namespace ClangBackEnd
......@@ -31,6 +31,8 @@
#include "clangreparsesupportivetranslationunitjob.h"
#include "clangrequestdocumentannotationsjob.h"
#include "clangrequestreferencesjob.h"
#include "clangresumedocumentjob.h"
#include "clangsuspenddocumentjob.h"
#include "clangupdatedocumentannotationsjob.h"
Q_LOGGING_CATEGORY(jobsLog, "qtc.clangbackend.jobs");
......@@ -54,6 +56,10 @@ IAsyncJob *IAsyncJob::create(JobRequest::Type type)
return new RequestDocumentAnnotationsJob();
case JobRequest::Type::RequestReferences:
return new RequestReferencesJob();
case JobRequest::Type::SuspendDocument:
return new SuspendDocumentJob();
case JobRequest::Type::ResumeDocument:
return new ResumeDocumentJob();
}
return nullptr;
......
......@@ -179,6 +179,16 @@ static bool passesPreconditions(const JobRequest &request, const Document &docum
using Condition = JobRequest::Condition;
const JobRequest::Conditions conditions = request.conditions;
if (conditions.testFlag(Condition::DocumentSuspended) && !document.isSuspended()) {
qCDebug(jobsLog) << "Not choosing due to unsuspended document:" << request;
return false;
}
if (conditions.testFlag(Condition::DocumentUnsuspended) && document.isSuspended()) {
qCDebug(jobsLog) << "Not choosing due to suspended document:" << request;
return false;
}
if (conditions.testFlag(Condition::DocumentVisible) && !document.isVisibleInEditor()) {
qCDebug(jobsLog) << "Not choosing due to invisble document:" << request;
return false;
......
......@@ -40,6 +40,8 @@ static const char *JobRequestTypeToText(JobRequest::Type type)
RETURN_TEXT_FOR_CASE(CompleteCode);
RETURN_TEXT_FOR_CASE(RequestDocumentAnnotations);
RETURN_TEXT_FOR_CASE(RequestReferences);
RETURN_TEXT_FOR_CASE(SuspendDocument);
RETURN_TEXT_FOR_CASE(ResumeDocument);
}
return "UnhandledJobRequestType";
......@@ -121,7 +123,19 @@ JobRequest::ExpirationReasons JobRequest::expirationReasonsForType(Type type)
JobRequest::Conditions JobRequest::conditionsForType(JobRequest::Type type)
{
Conditions conditions = Condition::DocumentVisible;
if (type == Type::SuspendDocument) {
return Conditions(Condition::DocumentUnsuspended)
| Conditions(Condition::DocumentNotVisible);
}
if (type == Type::ResumeDocument) {
return Conditions(Condition::DocumentSuspended)
| Conditions(Condition::DocumentVisible);
}
Conditions conditions = Conditions(Condition::DocumentUnsuspended)
| Conditions(Condition::DocumentVisible);
if (type == Type::RequestReferences)
conditions |= Condition::CurrentDocumentRevision;
......
......@@ -53,13 +53,18 @@ public:
CompleteCode,
RequestDocumentAnnotations,
RequestReferences,
SuspendDocument,
ResumeDocument,
};
enum class Condition {
NoCondition = 1 << 0,
DocumentVisible = 1 << 1,
DocumentNotVisible = 1 << 2,
CurrentDocumentRevision = 1 << 3,
DocumentSuspended = 1 << 3,
DocumentUnsuspended = 1 << 4,
CurrentDocumentRevision = 1 << 5,
};
Q_DECLARE_FLAGS(Conditions, Condition)
......
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "clangresumedocumentjob.h"
#include <utils/qtcassert.h>
namespace ClangBackEnd {
void ResumeDocumentJob::finalizeAsyncRun()
{
if (context().isDocumentOpen()) {
if (QTC_GUARD(asyncResult().updateResult.hasReparsed()))
m_pinnedDocument.setIsSuspended(false);
}
UpdateDocumentAnnotationsJob::finalizeAsyncRun();
}
bool ResumeDocumentJob::isExpectedJobRequestType(const JobRequest &jobRequest) const
{
return jobRequest.type == JobRequest::Type::ResumeDocument;
}
TranslationUnitUpdateInput ResumeDocumentJob::createUpdateInput(const Document &document) const
{
TranslationUnitUpdateInput input = UpdateDocumentAnnotationsJob::createUpdateInput(document);
input.reparseNeeded = true;
return input;
}
} // namespace ClangBackEnd
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#pragma once
#include "clangasyncjob.h"
#include "clangdocument.h"
#include "clangupdatedocumentannotationsjob.h"
namespace ClangBackEnd {
class ResumeDocumentJob : public UpdateDocumentAnnotationsJob
{
public:
void finalizeAsyncRun() override;
private:
bool isExpectedJobRequestType(const JobRequest &jobRequest) const override;
TranslationUnitUpdateInput createUpdateInput(const Document &document) const override;
};
} // namespace ClangBackEnd
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of Qt Creator.
**
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
****************************************************************************/
#include "clangsuspenddocumentjob.h"
#include <clangbackendipc/clangbackendipcdebugutils.h>
#include <utils/qtcassert.h>
namespace ClangBackEnd {
static bool runAsyncHelper(const TranslationUnit &translationUnit)
{
TIME_SCOPE_DURATION("SuspendDocumentJobRunner");
return translationUnit.suspend();
}
IAsyncJob::AsyncPrepareResult SuspendDocumentJob::prepareAsyncRun()
{
const JobRequest jobRequest = context().jobRequest;
QTC_ASSERT(jobRequest.type == JobRequest::Type::SuspendDocument, return AsyncPrepareResult());
try {