Index: kdevplatform/project/CMakeLists.txt =================================================================== --- kdevplatform/project/CMakeLists.txt +++ kdevplatform/project/CMakeLists.txt @@ -15,6 +15,7 @@ abstractfilemanagerplugin.cpp filemanagerlistjob.cpp projectfiltermanager.cpp + projectwatcher.cpp interfaces/iprojectbuilder.cpp interfaces/iprojectfilemanager.cpp interfaces/ibuildsystemmanager.cpp Index: kdevplatform/project/abstractfilemanagerplugin.h =================================================================== --- kdevplatform/project/abstractfilemanagerplugin.h +++ kdevplatform/project/abstractfilemanagerplugin.h @@ -34,6 +34,8 @@ namespace KDevelop { class AbstractFileManagerPluginPrivate; +class ProjectController; +class AFMPBenchmark; /** * This class can be used as a common base for file managers. @@ -103,6 +105,11 @@ */ KDirWatch* projectWatcher( IProject* project ) const; + /** + * tell the plugin that the given @p project is going to be closed. + */ + void projectClosing( IProject* project ); + Q_SIGNALS: void reloadedFileItem(KDevelop::ProjectFileItem* file); void reloadedFolderItem(KDevelop::ProjectFolderItem* folder); @@ -118,6 +125,9 @@ private: const QScopedPointer d; friend class AbstractFileManagerPluginPrivate; +public: + friend class ProjectController; + friend class AFMPBenchmark; }; } Index: kdevplatform/project/abstractfilemanagerplugin.cpp =================================================================== --- kdevplatform/project/abstractfilemanagerplugin.cpp +++ kdevplatform/project/abstractfilemanagerplugin.cpp @@ -41,6 +41,7 @@ #include #include "projectfiltermanager.h" +#include "projectwatcher.h" #include "debug.h" #define ifDebug(x) @@ -89,7 +90,7 @@ const KIO::UDSEntryList& entries); void deleted(const QString &path); - void created(const QString &path); + void dirty(const QString &path); void projectClosing(IProject* project); void jobFinished(KJob* job); @@ -103,7 +104,7 @@ void removeFolder(ProjectFolderItem* folder); - QHash m_watchers; + QHash m_watchers; QHash > m_projectJobs; QVector m_stoppedFolders; ProjectFilterManager m_filters; @@ -129,14 +130,15 @@ delete m_watchers.take(project); #ifdef TIME_IMPORT_JOB if (timer.isValid()) { - qCDebug(FILEMANAGER) << "Deleting dir watcher took" << timer.elapsed() / 1000.0 << "seconds for project" << project->name(); + qCInfo(FILEMANAGER) << "Deleting dir watcher took" << timer.elapsed() / 1000.0 << "seconds for project" << project->name(); } #endif m_filters.remove(project); } KIO::Job* AbstractFileManagerPluginPrivate::eventuallyReadFolder(ProjectFolderItem* item) { + ProjectWatcher* watcher = m_watchers.value( item->project(), nullptr ); FileManagerListJob* listJob = new FileManagerListJob( item ); m_projectJobs[ item->project() ] << listJob; qCDebug(FILEMANAGER) << "adding job" << listJob << item << item->path() << "for project" << item->project(); @@ -147,6 +149,9 @@ q->connect( listJob, &FileManagerListJob::entries, q, [&] (FileManagerListJob* job, ProjectFolderItem* baseItem, const KIO::UDSEntryList& entries) { addJobItems(job, baseItem, entries); } ); + q->connect( listJob, &FileManagerListJob::watchDir, + q, [this, item, watcher] (const QString& path) { + watcher->addDir(path); }, Qt::QueuedConnection ); return listJob; } @@ -261,17 +266,17 @@ } } -void AbstractFileManagerPluginPrivate::created(const QString& path_) +void AbstractFileManagerPluginPrivate::dirty(const QString& path_) { - qCDebug(FILEMANAGER) << "created:" << path_; + qCDebug(FILEMANAGER) << "dirty:" << path_; QFileInfo info(path_); ///FIXME: share memory with parent const Path path(path_); const IndexedString indexedPath(path.pathOrUrl()); const IndexedString indexedParent(path.parent().pathOrUrl()); - QHashIterator it(m_watchers); + QHashIterator it(m_watchers); while (it.hasNext()) { const auto p = it.next().key(); if ( !p->projectItem()->model() ) { @@ -299,21 +304,6 @@ // also gets triggered for kate's backup files continue; } - foreach ( ProjectFolderItem* parentItem, p->foldersForPath(indexedParent) ) { - if ( info.isDir() ) { - ProjectFolderItem* folder = q->createFolderItem( p, path, parentItem ); - if (folder) { - emit q->folderAdded( folder ); - auto job = eventuallyReadFolder( folder ); - job->start(); - } - } else { - ProjectFileItem* file = q->createFileItem( p, path, parentItem ); - if (file) { - emit q->fileAdded( file ); - } - } - } } } @@ -334,7 +324,7 @@ const Path path(QUrl::fromLocalFile(path_)); const IndexedString indexed(path.pathOrUrl()); - QHashIterator it(m_watchers); + QHashIterator it(m_watchers); while (it.hasNext()) { const auto p = it.next().key(); if (path == p->path()) { @@ -450,6 +440,9 @@ job->removeSubDir(folder); } } + ProjectWatcher* watcher = m_watchers.value(folder->project(), nullptr); + Q_ASSERT(watcher); + watcher->removeDir(folder->path().toLocalFile()); folder->parent()->removeRow( folder->row() ); } @@ -491,14 +484,14 @@ ///TODO: check if this works for remote files when something gets changed through another KDE app if ( project->path().isLocalFile() ) { - d->m_watchers[project] = new KDirWatch( project ); + auto watcher = new ProjectWatcher(project, &d->m_filters); - connect(d->m_watchers[project], &KDirWatch::created, - this, [&] (const QString& path_) { d->created(path_); }); - connect(d->m_watchers[project], &KDirWatch::deleted, + // set up the signal handling; feeding the dirwatcher is handled by FileManagerListJob. + connect(watcher, &KDirWatch::dirty, + this, [&] (const QString& path_) { d->dirty(path_); }); + connect(watcher, &KDirWatch::deleted, this, [&] (const QString& path_) { d->deleted(path_); }); - - d->m_watchers[project]->addDir(project->path().toLocalFile(), KDirWatch::WatchSubDirs | KDirWatch:: WatchFiles ); + d->m_watchers[project] = watcher; } d->m_filters.add(project); @@ -675,6 +668,12 @@ return d->m_watchers.value( project, nullptr ); } +void AbstractFileManagerPlugin::projectClosing( IProject *project ) +{ + d->projectClosing(project); +} + + //END Plugin #include "moc_abstractfilemanagerplugin.cpp" Index: kdevplatform/project/filemanagerlistjob.h =================================================================== --- kdevplatform/project/filemanagerlistjob.h +++ kdevplatform/project/filemanagerlistjob.h @@ -52,6 +52,7 @@ void entries(FileManagerListJob* job, ProjectFolderItem* baseItem, const KIO::UDSEntryList& entries); void nextJob(); + void watchDir(const QString& path); private Q_SLOTS: void slotEntries(KIO::Job* job, const KIO::UDSEntryList& entriesIn ); Index: kdevplatform/project/filemanagerlistjob.cpp =================================================================== --- kdevplatform/project/filemanagerlistjob.cpp +++ kdevplatform/project/filemanagerlistjob.cpp @@ -28,6 +28,8 @@ #include #include +#include + using namespace KDevelop; FileManagerListJob::FileManagerListJob(ProjectFolderItem* item) @@ -95,6 +97,8 @@ if (m_aborted) { return; } + // signal that this directory has to be watched + emit watchDir(path.toLocalFile()); KIO::UDSEntryList results; std::transform(entries.begin(), entries.end(), std::back_inserter(results), [] (const QFileInfo& info) -> KIO::UDSEntry { KIO::UDSEntry entry; Index: kdevplatform/project/projectwatcher.h =================================================================== --- /dev/null +++ kdevplatform/project/projectwatcher.h @@ -0,0 +1,50 @@ +/*************************************************************************** + * This file is part of KDevelop * + * Copyright 2017 René Bertin * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Library General Public License as * + * published by the Free Software Foundation; either version 2 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#ifndef KDEVPLATFORM_PROJECTWATCHER_H +#define KDEVPLATFORM_PROJECTWATCHER_H + +#include "projectexport.h" + +#include + +namespace KDevelop { + +class IProject; +class ProjectFilterManager; + +class KDEVPLATFORMPROJECT_EXPORT ProjectWatcher : public KDirWatch +{ +public: + explicit ProjectWatcher(IProject* project, ProjectFilterManager* filter); + + void addDir(const QString& path, WatchModes watchModes = WatchDirOnly); + void removeDir(const QString& path); + + int size() const; + +private: + IProject* m_project; + ProjectFilterManager* m_filter; + int m_watchedCount; +}; + +} +#endif //KDEVPLATFORM_PROJECTWATCHER_H Index: kdevplatform/project/projectwatcher.cpp =================================================================== --- /dev/null +++ kdevplatform/project/projectwatcher.cpp @@ -0,0 +1,57 @@ +/*************************************************************************** + * This file is part of KDevelop * + * Copyright 2017 René Bertin * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Library General Public License as * + * published by the Free Software Foundation; either version 2 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * + ***************************************************************************/ + +#include "projectwatcher.h" +#include "iproject.h" +#include "projectfiltermanager.h" +#include "path.h" + +#include + +using namespace KDevelop; + +KDevelop::ProjectWatcher::ProjectWatcher(IProject* project, ProjectFilterManager* filter) + : KDirWatch(project) + , m_project(project) + , m_filter(filter) + , m_watchedCount(0) +{} + +void KDevelop::ProjectWatcher::addDir(const QString& path, WatchModes watchModes) +{ + if (m_filter->isValid(Path(path), true, m_project) && !contains(path)) { + KDirWatch::addDir(path, watchModes); + m_watchedCount += 1; + } +} + +void KDevelop::ProjectWatcher::removeDir(const QString& path) +{ + if (contains(path)) { + KDirWatch::removeDir(path); + m_watchedCount -= 1; + } +} + +int KDevelop::ProjectWatcher::size() const +{ + return m_watchedCount; +} + Index: kdevplatform/project/tests/CMakeLists.txt =================================================================== --- kdevplatform/project/tests/CMakeLists.txt +++ kdevplatform/project/tests/CMakeLists.txt @@ -21,3 +21,12 @@ KDev::Tests Qt5::QuickWidgets ) + +add_executable(abstractfilemanagerpluginimportbenchmark + abstractfilemanagerpluginimportbenchmark.cpp +) +ecm_mark_nongui_executable(abstractfilemanagerpluginimportbenchmark) +target_link_libraries(abstractfilemanagerpluginimportbenchmark + KDev::Project + KDev::Tests +) Index: kdevplatform/project/tests/abstractfilemanagerpluginimportbenchmark.cpp =================================================================== --- /dev/null +++ kdevplatform/project/tests/abstractfilemanagerpluginimportbenchmark.cpp @@ -0,0 +1,182 @@ +/* This file is part of KDevelop + Copyright 2017 Ren� J.V. Bertin + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public + License as published by the Free Software Foundation; either + version 2 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public License + along with this library; see the file COPYING.LIB. If not, write to + the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. +*/ + +#include +#include +#include + +#include +#include +#include + +#include + +#include +#include + +#include +#include +#include +#include +#include +#include + +using namespace KDevelop; + +namespace KDevelop { + +class TestFileManagerPlugin : public AbstractFileManagerPlugin +{ + Q_OBJECT +public: + TestFileManagerPlugin(QObject* parent = nullptr) + : AbstractFileManagerPlugin({}, parent) + {} + + using AbstractFileManagerPlugin::projectClosing; +}; + +class AFMPBenchmark : public QObject +{ + Q_OBJECT +public: + AFMPBenchmark(TestFileManagerPlugin* manager, const QString& path, QObject* parent) + : QObject(parent) + { + m_manager = manager; + m_project = new TestProject(Path(path)); + } + + void start() + { + m_projectNumber = s_numBenchmarksRunning++; + qInfo() << "Starting import of project" << m_project->path(); + m_timer.start(); + auto root = m_manager->import(m_project); + int elapsed = m_timer.elapsed(); + qInfo() << "\tcreating dirwatcher took" + << elapsed / 1000.0 << "seconds"; + auto import = m_manager->createImportJob(root); + QObject::connect(import, &KJob::finished, this, &AFMPBenchmark::projectImportDone); + m_timer.restart(); + import->start(); + } + + void projectClosing() + { + m_timer.restart(); + m_manager->projectClosing(m_project); + int elapsed = m_timer.elapsed(); + qInfo() << "\tclosing project" << m_projectNumber << ":" << elapsed / 1000.0 << "seconds"; + } + + TestFileManagerPlugin* m_manager; + TestProject* m_project; + QElapsedTimer m_timer; + int m_projectNumber; + + static int s_numBenchmarksRunning; + +Q_SIGNALS: + void finished(); + +private Q_SLOTS: + void projectImportDone(KJob* job) + { + Q_UNUSED(job); + int elapsed = m_timer.elapsed(); + ProjectWatcher* watcher = dynamic_cast(m_manager->projectWatcher(m_project)); + int watched = watcher ? watcher->size() : -1; + qInfo() << "imported project" << m_projectNumber + << "with" << m_project->fileSet().size() + << "files (watched:" << watched << "):" + << elapsed / 1000.0 << "seconds"; + + s_numBenchmarksRunning -= 1; + if (s_numBenchmarksRunning <= 0) { + emit finished(); + } + } + +}; + +int AFMPBenchmark::s_numBenchmarksRunning = 0; +} + +int main(int argc, char** argv) +{ + if (argc < 2) { + qWarning() << "Usage:" << argv[0] << "projectDir1 [...projectDirN]"; + return 1; + } + QCoreApplication app(argc, argv); + + AutoTestShell::init({"no plugins"}); + auto core = TestCore::initialize(Core::NoUi); + auto manager = new TestFileManagerPlugin(core); + + const char *kdwMethod[] = {"FAM", "Inotify", "Stat", "QFSWatch"}; + qInfo() << "KDirWatch backend:" << kdwMethod[KDirWatch().internalMethod()]; + + QList benchmarks; + + for (int i = 1 ; i < argc ; ++i) { + const QString path = QString::fromUtf8(argv[i]); + if (QFileInfo(path).isDir()) { + const auto benchmark = new AFMPBenchmark(manager, path, core); + benchmarks << benchmark; + QObject::connect(benchmark, &AFMPBenchmark::finished, + &app, [&benchmarks] { + for (auto benchmark : benchmarks) { + benchmark->projectClosing(); + } + qInfo() << "Done"; + QCoreApplication::instance()->quit(); + }); + if (qEnvironmentVariableIsSet("BENCHMARK_ORIGINAL_DIRWATCHER")) { + // benchmark the creation and deletion of the original dirwatcher: + KDirWatch *watcher = new KDirWatch(benchmark->m_project); + qInfo() << "Benchmarking KDirWatch for all of" << argv[i]; + benchmark->m_timer.start(); + watcher->addDir(benchmark->m_project->path().toLocalFile(), KDirWatch::WatchSubDirs | KDirWatch:: WatchFiles ); + int elapsed = benchmark->m_timer.elapsed(); + qInfo() << "\tfeeding the watcher:" << elapsed / 1000.0 << "seconds"; + benchmark->m_timer.restart(); + delete watcher; + elapsed = benchmark->m_timer.elapsed(); + qInfo() << "\tdeleting the watcher:" << elapsed / 1000.0 << "seconds"; + } + } + } + + if (benchmarks.isEmpty()) { + qWarning() << "no projects to import (arguments must be directories)"; + return 1; + } + + for (auto benchmark : benchmarks) { + benchmark->start(); + } + + return app.exec(); +} + +#include "abstractfilemanagerpluginimportbenchmark.moc" + +