Skip to content

Commit 3d9c685

Browse files
committed
OrcLib: SystemDetails: add GetPhysicalMemoryInstalledSize
Use GetPhysicallyInstalledSystemMemory if available or fallback on GetPhysicalMemoryAdjustedSize
1 parent 3b31870 commit 3d9c685

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/OrcLib/SystemDetails.cpp

+45
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,31 @@ bool IsRunningInXen()
125125
return true;
126126
}
127127

128+
Result<uint64_t> GetPhysicallyInstalledSystemMemoryApi()
129+
{
130+
using FnGetPhysicallyInstalledSystemMemory = BOOL(__stdcall*)(PULONGLONG);
131+
132+
static FnGetPhysicallyInstalledSystemMemory fn = nullptr;
133+
if (fn == nullptr)
134+
{
135+
fn = reinterpret_cast<FnGetPhysicallyInstalledSystemMemory>(
136+
::GetProcAddress(GetModuleHandleA("kernel32.dll"), "GetPhysicallyInstalledSystemMemory"));
137+
if (!fn)
138+
{
139+
return LastWin32Error();
140+
}
141+
}
142+
143+
uint64_t installed = 0;
144+
BOOL ret = fn(&installed);
145+
if (!ret)
146+
{
147+
return LastWin32Error();
148+
}
149+
150+
return installed;
151+
}
152+
128153
} // namespace
129154

130155
namespace Orc {
@@ -724,6 +749,26 @@ Result<uint64_t> SystemDetails::GetPhysicalMemoryAdjustedSize()
724749
return roundedToGB;
725750
}
726751

752+
// Attempt to get the real installed memory size (GlobalMemoryStatusEx exclude some bytes)
753+
Result<uint64_t> SystemDetails::GetPhysicalMemoryInstalledSize()
754+
{
755+
auto memory = ::GetPhysicallyInstalledSystemMemoryApi();
756+
if (memory)
757+
{
758+
return *memory * 1024;
759+
}
760+
761+
Log::Debug("Failed GetPhysicallyInstalledSystemMemory [{}]", memory);
762+
763+
memory = SystemDetails::GetPhysicalMemoryAdjustedSize();
764+
if (memory)
765+
{
766+
return *memory;
767+
}
768+
769+
return memory.error();
770+
}
771+
727772
HRESULT SystemDetails::GetPageSize(DWORD& dwPageSize)
728773
{
729774
HRESULT hr = E_FAIL;

src/OrcLib/SystemDetails.h

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class SystemDetails
7777

7878
static Result<MEMORYSTATUSEX> GetPhysicalMemory();
7979
static Result<uint64_t> GetPhysicalMemoryAdjustedSize();
80+
static Result<uint64_t> GetPhysicalMemoryInstalledSize();
8081

8182
static HRESULT GetPageSize(DWORD& dwPageSize);
8283
static HRESULT GetLargePageSize(DWORD& dwPageSize);

0 commit comments

Comments
 (0)