Move "memory priority" settings to Application class

This commit is contained in:
Chocobo1
2022-06-29 16:55:40 +08:00
parent be7cfb78de
commit fa26333a5b
7 changed files with 127 additions and 99 deletions

View File

@@ -129,6 +129,9 @@ Application::Application(int &argc, char **argv)
, m_storeFileLoggerAgeType(FILELOGGER_SETTINGS_KEY(u"AgeType"_qs))
, m_storeFileLoggerPath(FILELOGGER_SETTINGS_KEY(u"Path"_qs))
, m_storeMemoryWorkingSetLimit(SETTINGS_KEY(u"MemoryWorkingSetLimit"_qs))
#ifdef Q_OS_WIN
, m_processMemoryPriority(SETTINGS_KEY(u"ProcessMemoryPriority"_qs))
#endif
{
qRegisterMetaType<Log::Msg>("Log::Msg");
qRegisterMetaType<Log::Peer>("Log::Peer");
@@ -607,6 +610,7 @@ int Application::exec(const QStringList &params)
#endif
#ifdef Q_OS_WIN
applyMemoryPriority();
adjustThreadPriority();
#endif
@@ -822,6 +826,65 @@ void Application::applyMemoryWorkingSetLimit() const
#endif
#ifdef Q_OS_WIN
MemoryPriority Application::processMemoryPriority() const
{
return m_processMemoryPriority.get(MemoryPriority::BelowNormal);
}
void Application::setProcessMemoryPriority(const MemoryPriority priority)
{
if (processMemoryPriority() == priority)
return;
m_processMemoryPriority = priority;
applyMemoryPriority();
}
void Application::applyMemoryPriority() const
{
using SETPROCESSINFORMATION = BOOL (WINAPI *)(HANDLE, PROCESS_INFORMATION_CLASS, LPVOID, DWORD);
const auto setProcessInformation = Utils::Misc::loadWinAPI<SETPROCESSINFORMATION>(u"Kernel32.dll"_qs, "SetProcessInformation");
if (!setProcessInformation) // only available on Windows >= 8
return;
#if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
// this dummy struct is required to compile successfully when targeting older Windows version
struct MEMORY_PRIORITY_INFORMATION
{
ULONG MemoryPriority;
};
#define MEMORY_PRIORITY_LOWEST 0
#define MEMORY_PRIORITY_VERY_LOW 1
#define MEMORY_PRIORITY_LOW 2
#define MEMORY_PRIORITY_MEDIUM 3
#define MEMORY_PRIORITY_BELOW_NORMAL 4
#define MEMORY_PRIORITY_NORMAL 5
#endif
MEMORY_PRIORITY_INFORMATION prioInfo {};
switch (processMemoryPriority())
{
case MemoryPriority::Normal:
default:
prioInfo.MemoryPriority = MEMORY_PRIORITY_NORMAL;
break;
case MemoryPriority::BelowNormal:
prioInfo.MemoryPriority = MEMORY_PRIORITY_BELOW_NORMAL;
break;
case MemoryPriority::Medium:
prioInfo.MemoryPriority = MEMORY_PRIORITY_MEDIUM;
break;
case MemoryPriority::Low:
prioInfo.MemoryPriority = MEMORY_PRIORITY_LOW;
break;
case MemoryPriority::VeryLow:
prioInfo.MemoryPriority = MEMORY_PRIORITY_VERY_LOW;
break;
}
setProcessInformation(::GetCurrentProcess(), ProcessMemoryPriority, &prioInfo, sizeof(prioInfo));
}
void Application::adjustThreadPriority() const
{
// Workaround for improving responsiveness of qbt when CPU resources are scarce.

View File

@@ -118,6 +118,11 @@ public:
int memoryWorkingSetLimit() const override;
void setMemoryWorkingSetLimit(int size) override;
#ifdef Q_OS_WIN
MemoryPriority processMemoryPriority() const override;
void setProcessMemoryPriority(MemoryPriority priority) override;
#endif
#ifndef DISABLE_GUI
MainWindow *mainWindow() override;
#endif
@@ -151,6 +156,7 @@ private:
#endif
#ifdef Q_OS_WIN
void applyMemoryPriority() const;
void adjustThreadPriority() const;
#endif
@@ -183,6 +189,10 @@ private:
SettingValue<Path> m_storeFileLoggerPath;
SettingValue<int> m_storeMemoryWorkingSetLimit;
#ifdef Q_OS_WIN
SettingValue<MemoryPriority> m_processMemoryPriority;
#endif
#ifndef DISABLE_GUI
MainWindow *m_window = nullptr;
#endif

View File

@@ -28,6 +28,7 @@
#include "upgrade.h"
#include <QtGlobal>
#include <QMetaEnum>
#include "base/bittorrent/torrentcontentlayout.h"
@@ -44,7 +45,7 @@
namespace
{
const int MIGRATION_VERSION = 3;
const int MIGRATION_VERSION = 4;
const QString MIGRATION_VERSION_KEY = u"Meta/MigrationVersion"_qs;
void exportWebUIHttpsFiles()
@@ -368,6 +369,21 @@ namespace
}
}
}
#ifdef Q_OS_WIN
void migrateMemoryPrioritySettings()
{
auto *settingsStorage = SettingsStorage::instance();
const QString oldKey = u"BitTorrent/OSMemoryPriority"_qs;
const QString newKey = u"Application/ProcessMemoryPriority"_qs;
if (settingsStorage->hasKey(oldKey))
{
const auto value = settingsStorage->loadValue<QVariant>(oldKey);
settingsStorage->storeValue(newKey, value);
}
}
#endif
}
bool upgrade(const bool /*ask*/)
@@ -392,6 +408,11 @@ bool upgrade(const bool /*ask*/)
if (version < 3)
migrateProxySettingsEnum();
#ifdef Q_OS_WIN
if (version < 4)
migrateMemoryPrioritySettings();
#endif
version = MIGRATION_VERSION;
}