From c78fe0d8d4a894780890c83073bb474479dbd2ff Mon Sep 17 00:00:00 2001 From: malte_langkabel Date: Tue, 11 Apr 2017 13:42:20 +0200 Subject: [PATCH] logic: Use all available cores when setting indexer threads to 0 (issue #342) --- CMakeLists.txt | 1 + src/lib/project/Project.cpp | 11 ++++- src/lib/settings/ApplicationSettings.cpp | 2 +- .../QtProjectWizzardContentPreferences.cpp | 44 +++++++++++++++++-- .../QtProjectWizzardContentPreferences.h | 3 ++ src/lib_gui/utility/utilityApp.cpp | 6 +++ src/lib_gui/utility/utilityApp.h | 2 + 7 files changed, 64 insertions(+), 5 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 2065fa47..dcafb736 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -219,6 +219,7 @@ set_property( "${CMAKE_SOURCE_DIR}/src/lib" "${CMAKE_SOURCE_DIR}/src/external" "${CMAKE_SOURCE_DIR}/src/lib_license" + "${CMAKE_SOURCE_DIR}/src/lib_gui" "${CMAKE_BINARY_DIR}/src/lib_license" ) diff --git a/src/lib/project/Project.cpp b/src/lib/project/Project.cpp index fca8dfde..da7a7a60 100644 --- a/src/lib/project/Project.cpp +++ b/src/lib/project/Project.cpp @@ -34,6 +34,7 @@ #include "utility/scheduling/TaskSetValue.h" #include "utility/text/TextAccess.h" #include "utility/utility.h" +#include "utility/utilityApp.h" #include "utility/utilityString.h" #include "utility/Version.h" @@ -412,7 +413,15 @@ void Project::buildIndex(const std::set& filesToClean, bool fullRefres if (indexerCommandList->size() > 0) { - const size_t indexerThreadCount = ApplicationSettings::getInstance()->getIndexerThreadCount(); + int indexerThreadCount = ApplicationSettings::getInstance()->getIndexerThreadCount(); + if (indexerThreadCount <= 0) + { + indexerThreadCount = utility::getIdealThreadCount(); + if (indexerThreadCount <= 0) + { + indexerThreadCount = 4; // setting to some fallback value + } + } if (indexerThreadCount > 1) { diff --git a/src/lib/settings/ApplicationSettings.cpp b/src/lib/settings/ApplicationSettings.cpp index 9938b79c..b906b81c 100644 --- a/src/lib/settings/ApplicationSettings.cpp +++ b/src/lib/settings/ApplicationSettings.cpp @@ -228,7 +228,7 @@ bool ApplicationSettings::setIndexingFilePaths(const std::vector& inde int ApplicationSettings::getIndexerThreadCount() const { - return getValue("indexing/indexer_thread_count", 4); + return getValue("indexing/indexer_thread_count", 0); } void ApplicationSettings::setIndexerThreadCount(const int count) diff --git a/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPreferences.cpp b/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPreferences.cpp index 7da2a481..55748014 100644 --- a/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPreferences.cpp +++ b/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPreferences.cpp @@ -1,5 +1,6 @@ #include "qt/window/project_wizzard/QtProjectWizzardContentPreferences.h" +#include "qt/utility/utilityQt.h" #include "settings/ApplicationSettings.h" #include "utility/file/FileSystem.h" #include "utility/messaging/type/MessageSwitchColorScheme.h" @@ -92,7 +93,30 @@ void QtProjectWizzardContentPreferences::populate(QGridLayout* layout, int& row) addTitle("INDEXING", layout, row); // indexer threads - m_threads = addComboBox("Indexer threads", 1, 24, "Number of parallel threads used to index your projects.", layout, row); + const int minThreadCount = 0; + const int maxThreadCount = 24; + + m_threads = new QComboBox(this); + connect(m_threads, SIGNAL(activated(int)), this, SLOT(indexerThreadsChanges(int))); + for (int i = minThreadCount; i <= maxThreadCount; i++) + { + m_threads->insertItem(i, QString::number(i)); + } + + m_threadsInfoLabel = new QLabel(""); + utility::setWidgetRetainsSpaceWhenHidden(m_threadsInfoLabel); + + QHBoxLayout* hlayout = new QHBoxLayout(); + hlayout->setContentsMargins(0, 0, 0, 0); + hlayout->addWidget(m_threads); + hlayout->addWidget(m_threadsInfoLabel); + + QWidget* threadsWidget = new QWidget(); + threadsWidget->setLayout(hlayout); + + addLabelAndWidget("Indexer threads", threadsWidget, layout, row, Qt::AlignLeft); + addHelpButton("Number of parallel threads used to index your projects.\nWhen setting this to 0 Sourcetrail tries to use the ideal thread count for your computer.", layout, row); + row++; addGap(layout, row); @@ -230,7 +254,8 @@ void QtProjectWizzardContentPreferences::load() m_sourcetrailPort->setText(QString::number(appSettings->getSourcetrailPort())); m_pluginPort->setText(QString::number(appSettings->getPluginPort())); - m_threads->setCurrentIndex(appSettings->getIndexerThreadCount() - 1); + m_threads->setCurrentIndex(appSettings->getIndexerThreadCount()); // index and value are the same + indexerThreadsChanges(m_threads->currentIndex()); if (m_javaPath) { @@ -273,7 +298,7 @@ void QtProjectWizzardContentPreferences::save() int pluginPort = m_pluginPort->text().toInt(); if (pluginPort) appSettings->setPluginPort(pluginPort); - appSettings->setIndexerThreadCount(m_threads->currentIndex() + 1); + appSettings->setIndexerThreadCount(m_threads->currentIndex()); // index and value are the same if (m_javaPath) { @@ -323,6 +348,19 @@ void QtProjectWizzardContentPreferences::loggingEnabledChanged() m_verboseIndexerLoggingEnabled->setEnabled(m_loggingEnabled->isChecked()); } +void QtProjectWizzardContentPreferences::indexerThreadsChanges(int index) +{ + if (index == 0) + { + m_threadsInfoLabel->setText(("detected " + std::to_string(utility::getIdealThreadCount()) + " threads to be ideal.").c_str()); + m_threadsInfoLabel->show(); + } + else + { + m_threadsInfoLabel->hide(); + } +} + void QtProjectWizzardContentPreferences::addJavaPathDetection(QGridLayout* layout, int& row) { std::vector detectorNames = m_javaPathDetector->getWorkingDetectorNames(); diff --git a/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPreferences.h b/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPreferences.h index 9ce4e3a7..539a748a 100644 --- a/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPreferences.h +++ b/src/lib_gui/qt/window/project_wizzard/QtProjectWizzardContentPreferences.h @@ -3,6 +3,7 @@ #include #include +#include #include #include "qt/element/QtFontPicker.h" @@ -31,6 +32,7 @@ private slots: void javaPathDetectionClicked(); void mavenPathDetectionClicked(); void loggingEnabledChanged(); + void indexerThreadsChanges(int index); private: void addJavaPathDetection(QGridLayout* layout, int& row); @@ -65,6 +67,7 @@ private: QLineEdit* m_pluginPort; QComboBox* m_threads; + QLabel* m_threadsInfoLabel; std::shared_ptr m_javaPathDetector; std::shared_ptr m_mavenPathDetector; diff --git a/src/lib_gui/utility/utilityApp.cpp b/src/lib_gui/utility/utilityApp.cpp index 37f8265f..55e21bb2 100644 --- a/src/lib_gui/utility/utilityApp.cpp +++ b/src/lib_gui/utility/utilityApp.cpp @@ -1,6 +1,7 @@ #include "utility/utilityApp.h" #include +#include #include #include "utility/utilityString.h" @@ -35,3 +36,8 @@ ApplicationArchitectureType utility::getApplicationArchitectureType() #endif return APPLICATION_ARCHITECTURE_UNKNOWN; } + +int utility::getIdealThreadCount() +{ + return QThread::idealThreadCount(); +} diff --git a/src/lib_gui/utility/utilityApp.h b/src/lib_gui/utility/utilityApp.h index c14b498e..7978bffa 100644 --- a/src/lib_gui/utility/utilityApp.h +++ b/src/lib_gui/utility/utilityApp.h @@ -10,6 +10,8 @@ namespace utility std::string executeProcess(const std::string& command, const std::string& workingDirectory = ""); ApplicationArchitectureType getApplicationArchitectureType(); + + int getIdealThreadCount(); } #endif // UTILITY_APP_H