Skip to content

Commit

Permalink
fix(systemMemorySizeMb): Improve system memory detection on BSD systems
Browse files Browse the repository at this point in the history
- Added support for detecting total system memory on FreeBSD, OpenBSD, and NetBSD using `sysctl`.
- Updated `systemMemorySizeMb()` to use `unsigned long` instead of `int` for better accuracy, especially on systems with large amounts of RAM.
- Introduced a `constexpr unsigned long megabyte` to improve readability and avoid redundant calculations.
- Unified memory detection logic for BSD systems, using `HW_PHYSMEM64` when available for better precision.

Fixes incorrect memory reporting on BSD platforms where it previously returned `-1`.
  • Loading branch information
lbartoletti committed Feb 6, 2025
1 parent 6b4ffb3 commit 3e8ade5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion python/PyQt6/core/auto_generated/qgsapplication.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ Returns a string name of the operating system QGIS is running on.
.. seealso:: :py:func:`platform`
%End

static int systemMemorySizeMb();
static unsigned long systemMemorySizeMb();
%Docstring
Returns the size of the system memory (RAM) in megabytes.

Expand Down
2 changes: 1 addition & 1 deletion python/core/auto_generated/qgsapplication.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ Returns a string name of the operating system QGIS is running on.
.. seealso:: :py:func:`platform`
%End

static int systemMemorySizeMb();
static unsigned long systemMemorySizeMb();
%Docstring
Returns the size of the system memory (RAM) in megabytes.

Expand Down
33 changes: 25 additions & 8 deletions src/core/qgsapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ const QgsSettingsEntryStringList *QgsApplication::settingsSearchPathsForSVG = ne
#include <sys/sysinfo.h>
#endif

#if defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) || defined(Q_OS_NETBSD)
#include <sys/types.h>
#include <sys/sysctl.h>
#endif

#define CONN_POOL_MAX_CONCURRENT_CONNS 4

QObject *ABISYM( QgsApplication::mFileOpenEventReceiver ) = nullptr;
Expand Down Expand Up @@ -1422,8 +1427,9 @@ QString QgsApplication::osName()
#endif
}

int QgsApplication::systemMemorySizeMb()
unsigned long QgsApplication::systemMemorySizeMb()
{
constexpr unsigned long megabyte = 1024 * 1024;
#if defined(Q_OS_ANDROID)
return -1;
#elif defined(Q_OS_MAC)
Expand All @@ -1434,22 +1440,33 @@ int QgsApplication::systemMemorySizeMb()
memoryStatus.dwLength = sizeof( MEMORYSTATUSEX );
if ( GlobalMemoryStatusEx( &memoryStatus ) )
{
return memoryStatus.ullTotalPhys / ( 1024 * 1024 );
return memoryStatus.ullTotalPhys / megabyte;
}
else
{
return -1;
}
#elif defined(Q_OS_LINUX)
constexpr int megabyte = 1024 * 1024;
struct sysinfo si;
sysinfo( &si );
return si.totalram / megabyte;
#elif defined(Q_OS_FREEBSD)
return -1;
#elif defined(Q_OS_OPENBSD)
return -1;
#elif defined(Q_OS_NETBSD)
#elif defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) || defined(Q_OS_NETBSD)
unsigned long physmem;
size_t size = sizeof( physmem );
#ifdef HW_PHYSMEM64
// NetBSD and OpenBSD have HW_PHYSMEM and HW_PHYSMEM64
// Use 64 bit version
int mib[2] = {CTL_HW, HW_PHYSMEM64};
#else
// For FreeBSD
int mib[2] = {CTL_HW, HW_PHYSMEM};
#endif

if ( sysctl( mib, 2, &physmem, &size, nullptr, 0 ) == 0 )
{
return physmem / megabyte;
}

return -1;
#elif defined(Q_OS_UNIX)
return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsapplication.h
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ class CORE_EXPORT QgsApplication : public QApplication
*
* \since QGIS 3.26
*/
static int systemMemorySizeMb();
static unsigned long systemMemorySizeMb();

/**
* Returns the QGIS platform name, e.g., "desktop", "server", "qgis_process" or "external" (for external CLI scripts).
Expand Down
2 changes: 1 addition & 1 deletion src/core/qgsimagecache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ QgsImageCache::QgsImageCache( QObject *parent )
}
else
{
const int sysMemory = QgsApplication::systemMemorySizeMb();
const unsigned long sysMemory = QgsApplication::systemMemorySizeMb();
if ( sysMemory > 0 )
{
if ( sysMemory >= 32000 ) // 32 gb RAM (or more) = 500mb cache size
Expand Down

0 comments on commit 3e8ade5

Please sign in to comment.