From 25b3f2d1a6b14f0fe098fb79a3d034607e52deae Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sat, 2 Jul 2022 14:57:47 +0800 Subject: [PATCH] Raise priority of the main "event loop" thread The goal is to improve responsiveness of qbt when CPU resources are scarce. Instead of lowering libtorrent threads priority, it is chosen to raise main event loop thread priority to avoid getting messy with libtorrent internals. Also on Windows, threads doesn't inherit thread priority from the parent thread and it always use the default (normal) priority. PR #17278. --- src/app/application.cpp | 18 +++++++++++++++++- src/app/application.h | 7 ++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/app/application.cpp b/src/app/application.cpp index cde4ada94..1a43049c7 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -601,6 +601,10 @@ int Application::exec(const QStringList ¶ms) applyMemoryWorkingSetLimit(); #endif +#ifdef Q_OS_WIN + adjustThreadPriority(); +#endif + Net::ProxyConfigurationManager::initInstance(); Net::DownloadManager::initInstance(); IconProvider::initInstance(); @@ -771,7 +775,7 @@ void Application::shutdownCleanup(QSessionManager &manager) #endif #ifdef QBT_USES_LIBTORRENT2 -void Application::applyMemoryWorkingSetLimit() +void Application::applyMemoryWorkingSetLimit() const { const size_t MiB = 1024 * 1024; const QString logMessage = tr("Failed to set physical memory (RAM) usage limit. Error code: %1. Error message: \"%2\""); @@ -810,6 +814,18 @@ void Application::applyMemoryWorkingSetLimit() } #endif +#ifdef Q_OS_WIN +void Application::adjustThreadPriority() const +{ + // Workaround for improving responsiveness of qbt when CPU resources are scarce. + // Raise main event loop thread to be just one level higher than libtorrent threads. + // Also note that on *nix platforms there is no easy way to achieve it, + // so implementation is omitted. + + ::SetThreadPriority(::GetCurrentThread(), THREAD_PRIORITY_ABOVE_NORMAL); +} +#endif + void Application::cleanup() { // cleanup() can be called multiple times during shutdown. We only need it once. diff --git a/src/app/application.h b/src/app/application.h index 740445c67..392e59912 100644 --- a/src/app/application.h +++ b/src/app/application.h @@ -136,8 +136,13 @@ private: void processParams(const QStringList ¶ms); void runExternalProgram(const BitTorrent::Torrent *torrent) const; void sendNotificationEmail(const BitTorrent::Torrent *torrent); + #ifdef QBT_USES_LIBTORRENT2 - void applyMemoryWorkingSetLimit(); + void applyMemoryWorkingSetLimit() const; +#endif + +#ifdef Q_OS_WIN + void adjustThreadPriority() const; #endif #ifndef DISABLE_GUI