From 61d9d66825762e2a64840a135e5a464670c137f1 Mon Sep 17 00:00:00 2001 From: yccb <69589207+yccb@users.noreply.github.com> Date: Wed, 23 Mar 2022 17:51:12 -0400 Subject: [PATCH 1/2] count swaptotal in total ram (linux) --- libraries/systeminfo/src/sys_unix.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/libraries/systeminfo/src/sys_unix.cpp b/libraries/systeminfo/src/sys_unix.cpp index b309852269..1bda95d13c 100644 --- a/libraries/systeminfo/src/sys_unix.cpp +++ b/libraries/systeminfo/src/sys_unix.cpp @@ -48,15 +48,16 @@ uint64_t Sys::getSystemRam() { std::string token; #ifdef Q_OS_LINUX + uint64_t mem = 0; std::ifstream file("/proc/meminfo"); while(file >> token) { - if(token == "MemTotal:") + if(token == "MemTotal:" || token == "SwapTotal:") { - uint64_t mem; - if(file >> mem) + uint64_t temp_mem; + if(file >> temp_mem) { - return mem * 1024ull; + mem += temp_mem; } else { @@ -66,6 +67,7 @@ uint64_t Sys::getSystemRam() // ignore rest of the line file.ignore(std::numeric_limits::max(), '\n'); } + return mem * 1024ull; #elif defined(Q_OS_FREEBSD) char buff[512]; FILE *fp = popen("sysctl hw.physmem", "r"); @@ -78,8 +80,8 @@ uint64_t Sys::getSystemRam() return mem * 1024ull; } } -#endif return 0; // nothing found +#endif } bool Sys::isCPU64bit() From 439669157b01df7a33a8a7aefa33b3a40d1c09ad Mon Sep 17 00:00:00 2001 From: yccb <69589207+yccb@users.noreply.github.com> Date: Wed, 23 Mar 2022 21:31:03 -0400 Subject: [PATCH 2/2] add option to use swap (linux) --- launcher/Application.cpp | 1 + launcher/ui/pages/global/JavaPage.cpp | 24 ++++++++++++++++-- launcher/ui/pages/global/JavaPage.h | 2 ++ launcher/ui/pages/global/JavaPage.ui | 10 ++++++++ libraries/systeminfo/include/sys.h | 2 ++ libraries/systeminfo/src/sys_apple.cpp | 5 ++++ libraries/systeminfo/src/sys_unix.cpp | 35 +++++++++++++++++++++----- libraries/systeminfo/src/sys_win32.cpp | 5 ++++ 8 files changed, 76 insertions(+), 8 deletions(-) diff --git a/launcher/Application.cpp b/launcher/Application.cpp index 958c5e3d52..80ed0a0c2e 100644 --- a/launcher/Application.cpp +++ b/launcher/Application.cpp @@ -658,6 +658,7 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv) m_settings->registerSetting({"MinMemAlloc", "MinMemoryAlloc"}, 512); m_settings->registerSetting({"MaxMemAlloc", "MaxMemoryAlloc"}, 1024); m_settings->registerSetting("PermGen", 128); + m_settings->registerSetting("AllowSwap", false); // Java Settings m_settings->registerSetting("JavaPath", ""); diff --git a/launcher/ui/pages/global/JavaPage.cpp b/launcher/ui/pages/global/JavaPage.cpp index bd79f11afc..cb17d6afbb 100644 --- a/launcher/ui/pages/global/JavaPage.cpp +++ b/launcher/ui/pages/global/JavaPage.cpp @@ -37,9 +37,13 @@ JavaPage::JavaPage(QWidget *parent) : QWidget(parent), ui(new Ui::JavaPage) ui->setupUi(this); ui->tabWidget->tabBar()->hide(); - auto sysMiB = Sys::getSystemRam() / Sys::mebibyte; - ui->maxMemSpinBox->setMaximum(sysMiB); +#if !defined(Q_OS_LINUX) + ui->allowSwapCheckBox->setEnabled(false); + ui->allowSwapCheckBox->setVisible(false); +#endif + loadSettings(); + updateMemMax(); } JavaPage::~JavaPage() @@ -71,6 +75,7 @@ void JavaPage::applySettings() s->set("MaxMemAlloc", min); } s->set("PermGen", ui->permGenSpinBox->value()); + s->set("AllowSwap", ui->allowSwapCheckBox->isChecked()); // Java Settings s->set("JavaPath", ui->javaPathTextBox->text()); @@ -94,12 +99,27 @@ void JavaPage::loadSettings() ui->maxMemSpinBox->setValue(min); } ui->permGenSpinBox->setValue(s->get("PermGen").toInt()); + ui->allowSwapCheckBox->setChecked(s->get("AllowSwap").toBool()); // Java Settings ui->javaPathTextBox->setText(s->get("JavaPath").toString()); ui->jvmArgsTextBox->setText(s->get("JvmArgs").toString()); } +void JavaPage::updateMemMax() { + auto sysMiB = Sys::getSystemRam() / Sys::mebibyte; + + if (ui->allowSwapCheckBox->isChecked()) + sysMiB += Sys::getSystemSwap() / Sys::mebibyte; + + ui->maxMemSpinBox->setMaximum(sysMiB); +} + +void JavaPage::on_allowSwapCheckBox_clicked(bool checked) +{ + updateMemMax(); +} + void JavaPage::on_javaDetectBtn_clicked() { JavaInstallPtr java; diff --git a/launcher/ui/pages/global/JavaPage.h b/launcher/ui/pages/global/JavaPage.h index 8f9b332363..e1d5547ea3 100644 --- a/launcher/ui/pages/global/JavaPage.h +++ b/launcher/ui/pages/global/JavaPage.h @@ -58,9 +58,11 @@ class JavaPage : public QWidget, public BasePage private: void applySettings(); void loadSettings(); + void updateMemMax(); private slots: + void on_allowSwapCheckBox_clicked(bool clicked); void on_javaDetectBtn_clicked(); void on_javaTestBtn_clicked(); void on_javaBrowseBtn_clicked(); diff --git a/launcher/ui/pages/global/JavaPage.ui b/launcher/ui/pages/global/JavaPage.ui index b67e9994ed..af430f8b4d 100644 --- a/launcher/ui/pages/global/JavaPage.ui +++ b/launcher/ui/pages/global/JavaPage.ui @@ -132,6 +132,16 @@ + + + + Allow use of swap + + + Allows the usage of swap when specifying memory allocation. + + + diff --git a/libraries/systeminfo/include/sys.h b/libraries/systeminfo/include/sys.h index bd6e2486a2..8cd14f7ee6 100644 --- a/libraries/systeminfo/include/sys.h +++ b/libraries/systeminfo/include/sys.h @@ -57,6 +57,8 @@ DistributionInfo getDistributionInfo(); uint64_t getSystemRam(); +uint64_t getSystemSwap(); + bool isSystem64bit(); bool isCPU64bit(); diff --git a/libraries/systeminfo/src/sys_apple.cpp b/libraries/systeminfo/src/sys_apple.cpp index 6353b747b7..45ff4d0913 100644 --- a/libraries/systeminfo/src/sys_apple.cpp +++ b/libraries/systeminfo/src/sys_apple.cpp @@ -55,6 +55,11 @@ uint64_t Sys::getSystemRam() } } +uint64_t Sys::getSystemSwap() +{ + return 0; +} + bool Sys::isCPU64bit() { // not even going to pretend I'm going to support anything else diff --git a/libraries/systeminfo/src/sys_unix.cpp b/libraries/systeminfo/src/sys_unix.cpp index 1bda95d13c..b57bcf31c4 100644 --- a/libraries/systeminfo/src/sys_unix.cpp +++ b/libraries/systeminfo/src/sys_unix.cpp @@ -48,16 +48,15 @@ uint64_t Sys::getSystemRam() { std::string token; #ifdef Q_OS_LINUX - uint64_t mem = 0; std::ifstream file("/proc/meminfo"); while(file >> token) { - if(token == "MemTotal:" || token == "SwapTotal:") + if(token == "MemTotal:") { - uint64_t temp_mem; - if(file >> temp_mem) + uint64_t mem; + if(file >> mem) { - mem += temp_mem; + return mem * 1024ull; } else { @@ -67,7 +66,6 @@ uint64_t Sys::getSystemRam() // ignore rest of the line file.ignore(std::numeric_limits::max(), '\n'); } - return mem * 1024ull; #elif defined(Q_OS_FREEBSD) char buff[512]; FILE *fp = popen("sysctl hw.physmem", "r"); @@ -80,8 +78,33 @@ uint64_t Sys::getSystemRam() return mem * 1024ull; } } +#endif return 0; // nothing found +} + +uint64_t Sys::getSystemSwap() +{ + std::string token; +#ifdef Q_OS_LINUX + std::ifstream file("/proc/meminfo"); + while(file >> token) + { + if(token == "SwapTotal:") + { + uint64_t swap; + if(file >> swap) + { + return swap * 1024ull; + } + else + { + return 0; + } + } + file.ignore(std::numeric_limits::max(), '\n'); + } #endif + return 0; } bool Sys::isCPU64bit() diff --git a/libraries/systeminfo/src/sys_win32.cpp b/libraries/systeminfo/src/sys_win32.cpp index 430b87e474..3a1b528ee1 100644 --- a/libraries/systeminfo/src/sys_win32.cpp +++ b/libraries/systeminfo/src/sys_win32.cpp @@ -27,6 +27,11 @@ uint64_t Sys::getSystemRam() return (uint64_t)status.ullTotalPhys; } +uint64_t Sys::getSystemSwap() +{ + return 0; +} + bool Sys::isSystem64bit() { #if defined(_WIN64)